简体   繁体   English

使用jQuery动态更新下拉列表

[英]Dynamically update drop down list with jquery

I want to make a form with a drop down list that change its value base on an input value on the form. 我想制作一个带有下拉列表的表单,该表单会根据表单上的输入值来更改其值。 Here is what i have been trying but dropdown list is still empty. 这是我一直在尝试的方法,但下拉列表仍然为空。

My form 我的表格

    <form action="../action/subs/custompcorder.php/" method="post">

      <div> <input id="rating" name="rating" onchange="loadComputers()"/> </div>

        <select name="computer" id="computer"></select> 
</div> 

</form>

    <script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript">  </script>
    <script src="./action/scripts/global.js" type="text/javascript"></script>

query page pcdrop2.php 查询页面pcdrop2.php

<?php
include './action/db/connect6.php';

    $rating = $_POST['rating'];   
    $myData=mysql_query("SELECT * FROM Product WHERE product.rate='$rating';");
    while($record=mysql_fetch_array($myData)){    
      echo '<option value="'.$record['product_id'].'">'.$record['product'].'</option>';    
}

?>

js function to pull data from db global.js js函数从db global.js中提取数据

    function loadComputers() {
    $val = $('#rating').html();
    $.post('action/subs/pcdrop2.php', {
    rating: $val
    }, function (data) {
    $('#computer').html(data);
});
}

I think that this line of code: 我认为这行代码:

$val = $('#rating').html();

Should actually be: 实际上应该是:

$val = $('#rating').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM