1

尝试获取值以更改<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 的新手

4

1 回答 1

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>
于 2013-02-12T10:22:48.587 回答