根据第一个下拉列表的选择设置第二个框的下拉值

时间:2013-03-22 07:34:20

标签: java jquery html5

我有2个下拉声明如下:

    <label for="select-choice-1" class="select">Customer Product Name</label>
    <select name="select-choice-1" id="select-choice-1" onchange="selectTransport(this.value)">
        <option value="a">Polyvinyl Chloride</option>
        <option value="b">Thermosetting</option>
        <option value="c">Thermoplastic</option>
    </select>
    <label for="select-choice-2" class="select">SABIC Product ID</label>
    <select name="select-choice-2" id="select-choice-2">
        <option value="A">1731-Black</option>
        <option value="B">1731-Brown</option>
        <option value="C">2345-Blue</option>
    </select>
       //js function
function selectTransport(value){
    if(value==="Thermoplastic")
    {

       var theText = "2345-Blue";
$("#select-choice-2 option:contains(" + theText + ")").attr('selected', 'selected');


    }}

不知何故没有使用此选择第二个下拉菜单。如何在使用jquery函数选择第一个下拉值后第二个下拉列表中自动选择值?

3 个答案:

答案 0 :(得分:1)

尝试

 $(function(){
   $('#select-choice-1').change(function(){
      var sel = $(this).val();
      $('#select-choice-2').val(sel.toUpperCase());
   });
 });

<强>更新

<强> DEMO

function selectTransport(value){
//value return a,b,c
var val = $('#select-choice-1 [value="'+value+'"]').html();//.text()
  if(val==="Thermoplastic"){alert('test');
    var theText = "2345-Blue";
    $("#select-choice-2 option:contains(" + theText + ")").attr('selected', 'selected');
  }
}

答案 1 :(得分:0)

function selectTransport(value){
 if(value==="Thermoplastic")
 {

    var theText = "2345-Blue";
 $("#select-choice-2 option:contains(" + theText + ")").attr('selected', 'selected');
上述函数值中的

将是a,b,c而不是 Thermoplastic

所以

function selectTransport(value){
 if(value==="c")
 {
  ....
}

答案 2 :(得分:0)

通过使用jquery,您可以轻松完成任务,下面是代码:

    var ddlArray= new Array();
var ddl = document.getElementById('tierFormulaBase');
for (i = 0; i < ddl.options.length; i++) {
   ddlArray[i] = ddl .options[i].text;
}

$("#tierChangeType").change(function (e) {
    var e = document.getElementById("tierChangeType");
    var selectedValue = e.options[e.selectedIndex].value;
    var selectedText = e.options[e.selectedIndex].text;

    document.getElementById('tierFormulaBase').innerHTML = "";
    var src = document.getElementById('tierFormulaBase');
    var opt = document.createElement("option");
    if(selectedValue == 1)
    {
         opt.text = ddlArray[0];
        opt.value = selectedValue;
    }else if (selectedValue == 2)
    {
         opt.text = ddlArray[1];
        opt.value = selectedValue;
    }
    else if (selectedValue == 3)
    {
         opt.text = ddlArray[2];
        opt.value = selectedValue;
    }
    src.add(opt);
 });

现场演示和更多信息请点击此链接:click here to view code