如何通过ajax发送2个选择框动态生成的值以获得第3个选择框值

时间:2014-12-05 21:06:11

标签: javascript php jquery ajax

我试图根据前2个选择框选择(动态值)获得第3个选择框值;

jQuery ajax代码是(它只适用于机场选择框)

$("#airport").change(function() {
    var aid=$(this).val();
    var dataString = 'aid='+ aid;
        $.ajax
        ({
            type: "POST",
            url: "booking/findcompany.php",
            data: dataString,
            cache: false,
            success: function(data) {
                $("#company").html(data);
            } 
        });
});

<select name="site" id="site" class="site">
    <option value="" selected="selected">Select</option>
    <option value="1">Site One</option>
    <option value="2">Site Two</option>
    <option value="3">Site Three</option>
</select> 

<select name="airport" id="airport" class="airport">
    <option value="1">Airport One</option>
    <option value="2">Airport Two</option>
    <option value="3">Airport Three</option>
</select>  

<select name="company" id="company" class="company">
    //Options here based on above 2 selected values
</select>

PHP代码为findcompany.php

<?php
if($_POST['aid']) {
    $aid=$_POST['aid'];
    $site=$_POST['sid']; <<<<<< How Can I pass This ID
    $compsql=mysql_query("select * from tbl_company Where air_id='$aid' and site_id='$sid'");
    while($rows=mysql_fetch_array($compsql)){
        $cid=$rows['comp_id'];
        $cdata=$rows['comp_title'];
?>
    <option value="<?php echo $cid; ?>"><?php echo $cdata; ?></option>
<?php } } ?>

1 个答案:

答案 0 :(得分:1)

使用$('#site').val()获取第一个选择框的值,然后将其添加到dataString。所以现在你的js代码看起来像 -

$("#airport").change(function()
    {
    var aid=$(this).val();
    var sid=$('#site').val();
    var dataString = 'aid='+ aid +'&sid='+sid;
    ...