由 mysql php 填充的链接下拉菜单


Link dropdown menus populated by mysql php

如果我的术语是错误的,请原谅,因为我是开发新手,但我有一个链接的多个下拉菜单,以便上一个菜单的结果决定了下一个菜单将填充下一个菜单的内容。

即如果我选择英国,下一个列表将显示,

伦敦,如果我选择西班牙,下一个列表将显示马德里

我的下拉菜单被mysql填充,这很好,但是如何在不调用另一页的情况下链接它们

这是我拥有的HTML的示例:

<div id="container">
  <h3>Price List</h3>
  <form method="post" action="" name="form1">
  <? 
  include 'classes.php';
  $query="SELECT * FROM Sex_Table";
  $result=mysql_query($query);
  ?>
  <div id="select-1">
    <select class="calculate" name="sdf" onChange="getClothing(this.value)">
    <option value="0">Please Select</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['Price']?> sex_price="<?=$row['Price']?>"><?=$row['Sex_Name']?>&nbsp;(£<?=$row['Price']?>)</option>
  <? } ?>
    </select>
  </div>
  <? 
  $Sex=intval($_GET['Sex_var']);
  include 'classes.php';
  $query="SELECT * FROM Clothing_Table WHERE Sex_Table_ID='$Sex'";
  $result=mysql_query($query);
  ?>
  <div id="select-2">
    <select class="calculate" name="sdf" onChange="">
    <option value="0">Please Select</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['Price']?> sex_price="<?=$row['Price']?>"><?=$row['Clothing_Name']?>&nbsp;(£<?=$row['Price']?>)</option>
  <? } ?>
    </select>
  </div>
</form>

您会注意到,在更改时调用"getClothing(this.value)"

这是js:

function getClothing(Sex_Table_ID) {        
    $.ajax({
       type: "get",
       url: "findClothing.php",
       data: { Sex_var: Sex_Table_ID },
       error: function(error) { alert('System error, please try again.'); },
       success: function(data){ //data is the response from the called file
         $("#select-2").html(data); //this changes the contents of the div to data that was returned
       }
     });    
}

我需要它,这样它就不会调用网址:"findClothing.php",因为一旦它这样做,我的价格就不再计算了。

确定我真的不需要显示这部分,但以防万一,这是计算我的价格的原因:

$(function(){
    $('.calculate').change(function() {
        var total = 0;
        $('.calculate').each(function() {
            if($(this).val() != 0) {
                total += parseFloat($(this).val());
            }
        });
        $('#total').text('£' + total.toFixed(2));
    });
});//]]>

您确定您的 ajax 数据包含以 price 作为值的选项标签吗?

还更改:

$('.calculate').change(function() {

$(".calculate").live("change", function(){