0

我有 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 函数在选择第一个下拉值后自动选择第二个下拉列表中的值?

4

3 回答 3

1

尝试

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

更新

演示

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');
  }
}
于 2013-03-22T07:41:24.093 回答
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);
 });

有关现场演示和更多信息,请访问此链接:单击此处查看代码

于 2013-09-21T10:11:47.983 回答
0
function selectTransport(value){
 if(value==="Thermoplastic")
 {

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

in above function value would be either a,b,c not Thermoplastic

so

function selectTransport(value){
 if(value==="c")
 {
  ....
}
于 2013-03-22T09:20:02.840 回答