【问题标题】:Display selected option in PHP MySQL在 PHP MySQL 中显示选定的选项
【发布时间】:2020-06-09 19:01:05
【问题描述】:

我想显示选择的程序选项(selectedprg)并执行程序的计数(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>

screencapture

【问题讨论】:

  • 一方面,value="'.$row['student_prg'].' 缺少结束引号,data-count='.$row['count'].'" 缺少开始引号。
  • 我已经编辑了上面的引用,@kerbh0lz ...但点击按钮后仍然没有输出。

标签: javascript php html mysql


【解决方案1】:

注意事项:

  • selectedprgselectedcount 不是全局可用的,你不能在你的函数中访问它们。将它们移动/声明到外面。
  • 您的 $(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">
  var selectedprg = '';
  var selectedcount = 0;

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

  $(document).ready(function(){
    $("#prg").change( function(){
      selectedprg = $('#prg option:selected').text(); 
      selectedcount = $('#prg option:selected').data('count');
    });
  });
</script>

【讨论】:

    猜你喜欢
    • 2022-12-19
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多