2017-11-04 88 views
0

基於此代碼:http://jsfiddle.net/3UWk2/3/自動選擇第一個下拉選項

<select size="1" id="Rank" title="" name="Rank"> 
<option value="">-Select Your Rank-</option> 
<option value="airman">Airman</option> 
<option value="senior-airman">Senior Airman</option> 
</select> 

<div class="container"> 
<div class="airman"> 
    <select class="second-level-select"> 
     <option value="">-Select Your Rank-</option> 
     <option value="basic-ore-1">Basic Ore Miner - Level 1</option> 
     <option value="basic-ore-2">Basic Ore Miner - Level 2</option> 
    </select> 
</div> 
<div class="senior-airman"> 
    <select class="second-level-select"> 
     <option value="">-Select Your Rank-</option> 
     <option value="omber-miner-1">Omber Miner - Level 1</option> 
     <option value="omber-miner-2">Omber Miner - Level 2</option> 
    </select> 
</div> 
</div> 

<div class="second-level-container"> 
<div class="basic-ore-1"> 
    Line of text for basic ore miner 1 
</div> 
<div class="basic-ore-2"> 
    Line of text for basic ore miner 2 
</div> 
<div class="omber-miner-1"> 
    Line of text for omber miner 1 
</div> 
<div class="omber-miner-2"> 
    Line of text for omber miner 2 
</div>  
</div> 

依次爲:

$(document).ready(function() { 
$('#Rank').bind('change', function() { 
    var elements = $('div.container').children().hide(); // hide all the elements 
    var value = $(this).val(); 

    if (value.length) { // if somethings' selected 
     elements.filter('.' + value).show(); // show the ones we want 
    } 
}).trigger('change'); 

$('.second-level-select').bind('change', function() { 
    var elements = $('div.second-level-container').children().hide(); // hide all the elements 
    var value = $(this).val(); 

    if (value.length) { // if somethings' selected 
     elements.filter('.' + value).show(); // show the ones we want 
    } 
}).trigger('change'); 
}); 

如何自動選擇兩個下拉列表的第一個選項,顯示最終值的OnLoad?

我正在尋找3乘3下降,共有9個值。

+0

不可能得到「顯示最終值的onLoad」的一部分,你想與3個下拉菜單相同的解決方案,你想同時下降的第一個選項在頁面加載和相關的行文本中自動選擇下拉菜單? –

+0

我希望自動選擇下拉菜單的第一個選項並顯示最終值。 同樣在選擇後,更改第一個下拉菜單不會影響最終值,直到再次選擇第二個下拉菜單,任何解決此問題的方法? 感謝您的及時回覆 –

回答

0

檢查,這可能幫助你

$(document).ready(function() { 
 
//bind onchange for the rank dd 
 
    $("#Rank").on('change', function() { 
 
    let selectedValue = $(this).val(); 
 
    $(".container>div").hide(); 
 
    if (selectedValue.length) { 
 
     $("." + selectedValue).show(); 
 
     //select.val($(select + " option:eq(1)").val()); 
 
    } 
 
    }) 
 
//bind onchange for the airman dd 
 
    $("#second-level-select-airman").on('change', function() { 
 
    let selectedValue = $(this).val(); 
 
    $(".second-level-container").children().hide(); 
 

 

 
    if (selectedValue.length) { 
 
     $("." + selectedValue).show(); 
 
     //select.val($(select + " option:eq(1)").val()); 
 
    } 
 
    }) 
 
//bind onchange for the senior airman 
 
    $("#second-level-select-senior-airman").on('change', function() { 
 
    let selectedValue = $(this).val(); 
 
    $(".second-level-container").children().hide(); 
 

 

 
    if (selectedValue.length) { 
 
     $("." + selectedValue).show(); 
 
     //select.val($(select + " option:eq(1)").val()); 
 
    } 
 
    }); 
 
    //select first value of first dropdown 
 
    $("#Rank").val($("#Rank option:eq(1)").val()).trigger('change'); 
 
    
 
    //select the first option from the 2nd menu depending on the valur of the first dd 
 
    $("." + $("#Rank").val()).find("select").val(
 
    $("." + $("#Rank").val()).find("select option:eq(1)").val() 
 
).trigger('change'); 
 
    // $("#Rank").val($("#Rank option:eq(1)").val()); 
 
    // $("." + $("#Rank").val()).find("select").val(
 
    //  $("." + $("#Rank").val()).find("select option:eq(1)").val() 
 
    //); 
 
});
.airman, 
 
.senior-airman, 
 
.second-level-container>div { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id="Rank" name="Rank"> 
 
      <option value="">-Select Your Rank-</option> 
 
      <option value="airman">Airman</option> 
 
      <option value="senior-airman">Senior Airman</option> 
 
     </select> 
 

 
<div class="container"> 
 
    <div class="airman"> 
 
    <select class="second-level-select" id="second-level-select-airman"> 
 
        <option value="">-Select Your Rank-</option> 
 
        <option value="basic-ore-1">Basic Ore Miner - Level 1</option> 
 
        <option value="basic-ore-2">Basic Ore Miner - Level 2</option> 
 
       </select> 
 
    </div> 
 
    <div class="senior-airman"> 
 
    <select class="second-level-select" id="second-level-select-senior-airman"> 
 
        <option value="">-Select Your Rank-</option> 
 
        <option value="omber-miner-1">Omber Miner - Level 1</option> 
 
        <option value="omber-miner-2">Omber Miner - Level 2</option> 
 
       </select> 
 
    </div> 
 
</div> 
 

 
<div class="second-level-container"> 
 
    <div class="basic-ore-1"> 
 
    Line of text for basic ore miner 1 
 
    </div> 
 
    <div class="basic-ore-2"> 
 
    Line of text for basic ore miner 2 
 
    </div> 
 
    <div class="omber-miner-1"> 
 
    Line of text for omber miner 1 
 
    </div> 
 
    <div class="omber-miner-2"> 
 
    Line of text for omber miner 2 
 
    </div> 
 
</div>

+0

謝謝你的工作很棒! 但是,如果用戶要將第一個下拉列表更改爲下拉列表,則在選擇顯示的下拉列表後,該值不會更改。任何方式來解決它? –

+0

做選擇正確的答案,如果它適合你,以便尋找相同的解決方案的其他人也可以得到好處@Jacky –

+0

好吧,但你能幫我解決我以前的評論部分?也似乎沒有正確顯示在手機上 –

相關問題