【问题标题】:set drop down value of second box depending on selection of first drop down根据第一个下拉菜单的选择设置第二个框的下拉值
【发布时间】:2013-09-21 18:11:47
【问题描述】:

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

【问题讨论】:

  • 两个选择都有相同的id

标签: java jquery html


【解决方案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');
  }
}

【讨论】:

  • 我已经编辑了我的问题并添加了我正在尝试的内容,但它不起作用。
  • 嗨,Pragnesh,您的代码所做的是,当我查看列表时,它会在第二个下拉菜单中突出显示 2345-blue,但它没有在框中显示值 2345-blue。
  • 。是的,我确实在 fiddle 上看到了代码,它工作正常,但我正在使用 icenium,并且下拉项目被选中而不显示。所以我必须刷新下拉下来还是什么?
  • @user2185012,请检查html或代码中可能存在一些错误/问题
  • ,如果我添加 $("#select-choice-2").selectmenu("refresh");然后它反映了变化。谢谢。
【解决方案2】:
function selectTransport(value){
 if(value==="Thermoplastic")
 {

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

在上述函数值中,a、b、c 不是 热塑性

所以

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

【讨论】:

    【解决方案3】:

    使用 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

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2021-12-11
      • 2019-02-14
      • 2021-09-30
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多