获取要在Jquery的多选框的onchange中选择的值

时间:2013-02-12 08:24:02

标签: php javascript jquery html

在其他<select>代码值的更改时尝试获取值<select>。我有两个名为vehicle_type和part_type的下拉列表。选择车辆类型和零件类型时我想做什么需要更改零件名称下拉列表。它与车型和零件类型组合不同。所以我写了下面的代码。有人可以帮我找到这段代码的错误。我是jquery的新手

我的HTML

<div>
                    <label>Vehicle Type</label>
                    <label>
                        <select name="vehicle_type" id="vehicle_type" required>
                          <option value='' disabled selected style='display:none;'>Select Vehicle</option>
                          <option value="Car">Car</option>
                          <option value="Van">Van</option>
                          <option value="SUV/4WD">SUV/4WD/CAB</option>
                          <option value="Truck">Truck</option>
                        </select>
                    </label>
                </div>
                <div>
                    <label>Part Type</label>
                    <label>
                        <select name="part_type" id="part_type" required>
                            <option value='' disabled selected style='display:none;'>Select Part Type</option>
                            <option value="Engine">Engine</option>
                            <option value="Body">Body</option>
                        </select>
                    </label>
                </div>
                <div>
                    <label>Part Name</label>
                    <label>
                        <select name="part_name" id="part_name" required>
                            <option value='' disabled selected style='display:none;'>Select Part Name</option>
                        </select>
                    </label>
                </div>

#dropdown.js
function setSelect(vehicle_type, part_type){
$.post("dropdown.php",{"vehicle_type":vehicle_type, "part_type":part_type},function(data){
    $("#part_name").html(data);
});
}

$(document).ready(function(){

$("#vehicle_type").live('change', function(){
    var vehicle = $(this+"option:selected").val();
});

$("#part_type").live('change', function(){
    var part = $(this+"option:selected").val();     
});

setSelect($("#vehicle_type option:selected").val(), $("#part_type option:selected").val());
});

我的dropdown.php

<?php
if(isset($_POST['vehicle_type'])&&isset($_POST['part_type'])){

    $vehicle_type =$_POST['vehicle_type'];
    $part_type    =$_POST['part_type'];

    if(($vehicle_type=="Car")&&($part_type=="Engine")){
        $partname  = "<option value=\"Engine Head Block\">Engine Head Block</option>";
        $partname .= "<option value=\"Engine Complete W/O Gear Box\">Engine Complete W/O Gear Box</option>";
        $partname .= "<option value=\"Engine With A/T CVT Gear Box\">Engine With A/T CVT Gear Box</option>";
} elseif(($vehicle_type=="Car")&&($part_type=="Body")){
        $partname  = "<option value=\"Nose Pannel / Parts\">Nose Pannel / Parts</option>";
        $partname .= "<option value=\"Front Bumper With Fog Lamp L/R\">Front Bumper With Fog Lamp L/R</option>";
        $partname .= "<option value=\"Front Bumper Bracket Retainer L/R\">Front Bumper Bracket Retainer L/R</option>";
}

请告诉我错误。我是jquery的新手

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php
if(isset($_POST['vehicle_type'])&&isset($_POST['part_type'])){

$vehicle_type =$_POST['vehicle_type'];
$part_type    =$_POST['part_type'];

if(($vehicle_type=="Car")&&($part_type=="Engine")){
    $partname  = "<option value=\"Engine Head Block\">Engine Head Block</option>";
    $partname .= "<option value=\"Engine Complete W/O Gear Box\">Engine Complete W/O Gear Box</option>";
    $partname .= "<option value=\"Engine With A/T CVT Gear Box\">Engine With A/T CVT Gear Box</option>";
 } elseif(($vehicle_type=="Car")&&($part_type=="Body")){
    $partname  = "<option value=\"Nose Pannel / Parts\">Nose Pannel / Parts</option>";
    $partname .= "<option value=\"Front Bumper With Fog Lamp L/R\">Front Bumper With Fog Lamp L/R</option>";
    $partname .= "<option value=\"Front Bumper Bracket Retainer L/R\">Front Bumper Bracket Retainer L/R</option>";
 }
 echo $partname;
}
?>

无需更改HTML。只需将JS更改为

<script type="text/javascript">

function setSelect(vehicle_type, part_type){
//alert(vehicle_type+part_type);
 $.post("dropdown.php",{"vehicle_type":vehicle_type, "part_type":part_type},function(data){
  $("#part_name").html(data);

 });
 }

 $(document).ready(function(){

 $("#vehicle_type").change(function(){
var vehicle = $("option:selected",this).val();

 });

 $("#part_type").change(function(){
 var part = $("option:selected",this).val();   
 setSelect($("#vehicle_type option:selected").val(), $("#part_type option:selected").val());
 });


  });
</script>