猿问

在 PHP MySQL 中显示选定的选项

我想显示选择的程序选项(selectedprg)并执行程序的COUNT(selectedcount)但目前点击按钮后没有数据显示。请协助解决这个问题。感谢您的时间。


下拉选择


<select id="prg" name="prg"  class="demoInputBox" style="padding: 3px; width: 350px;">

    <option value="">Select Programme</option>


        <?php

        //"SELECT * FROM marketing_data WHERE intake_year = '" . $_GET['intake_year'] . "'";

        $sql = "SELECT DISTINCT student_prg FROM marketing_data";

        $do = mysqli_query($conn, $sql);

                          while($row = mysqli_fetch_array($do)){

                              echo '<option value="'.$row['student_matric'].'" data-count="'.$row['count'].'">'.$row['student_prg'].'</option>';

                          }

                      ?>

    </select>

点击按钮后显示数据


<button onclick="myFunction()">Click me</button>


<p id="demo"></p>


<script>

function myFunction() {

  document.getElementById("demo").innerHTML = "The next intake for " +selectedprg + " will have " +selectedcount+ " students";

}

</script>

Javascript


<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> 

<script type="text/javascript">

$(document).ready(function(){


$("#prg").change(function(){

 var selectedprg = $('#prg option:selected').text(); 

var selectedcount = $('#prg option:selected').data('count');


</script>

截屏


狐的传说
浏览 114回答 1
1回答

慕雪6442864

一些注意事项:selectedprg并且selectedcount不是全局可用的,你不能在你的函数中访问它们。将它们移动/声明在外面。你的$(document).ready(function(){和$("#prg").change( function(){缺少他们的结束})部分。使用浏览器的 DevTools 控制台检查错误。尝试这个:<button onclick="myFunction()">Click me</button><p id="demo"></p><script src="//code.jquery.com/jquery-1.12.0.min.js"></script><script type="text/javascript">&nbsp; var selectedprg = '';&nbsp; var selectedcount = 0;&nbsp; function myFunction() {&nbsp; &nbsp; document.getElementById("demo").innerHTML = "The next intake for " +selectedprg + " will have " +selectedcount+ " students";&nbsp; }&nbsp; $(document).ready(function(){&nbsp; &nbsp; $("#prg").change( function(){&nbsp; &nbsp; &nbsp; selectedprg = $('#prg option:selected').text();&nbsp;&nbsp; &nbsp; &nbsp; selectedcount = $('#prg option:selected').data('count');&nbsp; &nbsp; });&nbsp; });</script>
随时随地看视频慕课网APP
我要回答