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');


    }}

どういうわけか、これを使用して2番目のドロップダウンが選択されていません.jquery関数を使用して、最初のドロップダウン値が選択された後、2番目のドロップダウンの値を自動的に選択するにはどうすればよいですか?

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
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")
 {
  ....
}
于 2013-03-22T09:20:02.840 に答える
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 に答える