简体   繁体   English

每当我在第一个下拉菜单中更改选项时,都需要jquery将第二个下拉菜单的默认选项更改为“选择”

[英]need jquery to change the default option of 2nd dropdown to “select” whenever i change options in 1st dropdown

This is the the jquery i am using. 这是我正在使用的jQuery。 I am able to get "select" in district when i choose state but when i keep selecting different state again and again without touching district the values in district changes to the last option of each district. 当我选择状态时,我可以在区域中获得“选择”,但是当我一次又一次地选择不同的状态而又不触及区域时,区域中的值将更改为每个区域的最后一个选项。 I want default to be "select"(disabled) each time i change state. 我希望每次更改状态时默认为“选择”(禁用)。

<script>
  $(document).ready(function() {
    $('#select3').selectmenu('disable');
    $("#select2").change(function() {
      if ($(this).data('options') === undefined) {
        $(this).data('options', $('#select3 option').clone());
      }
      var id = $(this).val();
      var option = $(this).data('options').filter('[value=' + id + ']');
      $('#select3').html(option);
      $('#select3').selectmenu('refresh');
      $('#select3').selectmenu('enable');
    });
  }); 

 <body>
          <label for="select2">State:</label>
          <select name="select2" id="select2">
                <option value = "0">Select</option>
                <option value = "1">Maharashtra</option>
                <option value = "2">Gujarat</option>
                <option value = "3">Rajasthan</option>
           </select>

        <label for="select3">District:</label>
        <select name="select3" id="select3">
               <option value = "0">Select State</option>
               <option value = "1">Select</option>
               <option value = "1">Pune</option>                       
               <option value = "1">Vidarbha</option>
               <option value = "1">Thane</option>
               <option value = "2">Select</option>
               <option value = "2">Bharuch</option>
               <option value = "2">Ahmedabad</option>
               <option value = "2">Jamnagar</option>
               <option value = "3">Select</option>
               <option value = "3">Jaipur</option>
               <option value = "3">Jodhpur</option>
               <option value = "3">Bikaner</option>
           </select>
</body>

You can use CSS alone to hide the other values on page load, and then filter through the select3 options. 您可以单独使用CSS来隐藏页面加载时的其他值,然后通过select3选项进行过滤。 (You can't use your $(this).data('options').filter because this refers to #select2, not #select3, so the states options remain unchanged) (您不能使用$(this).data('options').filter因为this引用的是#select2,而不是#select3,因此状态选项保持不变)

 const select2 = $("#select2"); select2.change(() => { const stateVal = select2.val(); const select3Options = $('#select3').children(); select3Options.show(); select3Options.filter((_, elm) => elm.value && elm.value !== stateVal).hide(); select3Options[0].selected = 'selected'; }); 
 #select3 > option:not([value="0"]) { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <label for="select2">State:</label> <select name="select2" id="select2"> <option value = "0">Select</option> <option value = "1">Maharashtra</option> <option value = "2">Gujarat</option> <option value = "3">Rajasthan</option> </select> <label for="select3">District:</label> <select name="select3" id="select3"> <option value = "0">Select State</option> <option value = "1">Pune</option> <option value = "1">Vidarbha</option> <option value = "1">Thane</option> <option value = "2">Bharuch</option> <option value = "2">Ahmedabad</option> <option value = "2">Jamnagar</option> <option value = "3">Jaipur</option> <option value = "3">Jodhpur</option> <option value = "3">Bikaner</option> </select> </body> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery:根据第一个下拉菜单的选择更改第二个下拉菜单的选择 - jQuery: Change selection of 2nd dropdown based on selection of 1st 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 基于第一个下拉列表的动态第二个下拉列表 - Dynamic 2nd Dropdown-list based on the 1st Dropdown 根据第一个下拉值清除第二个下拉值 - Clear 2nd Dropdown values based on 1st Dropdown value 根据PHP中选择的第一个下拉列表填充第二个下拉列表 - Populate 2nd dropdown based on 1st dropdown selected in PHP 如何 - 使第一个动态下拉列表必须从第二个下拉列表中进行选择? - How to - Make 1st dynamic dropdown MUST to select from 2nd dropdown? 如何在选项标签 select 下拉列表 1st 2nd 3rd 4th 5th 中添加上标。 1ST 我想要 S 大写字母,但我试过 s 将转换为小写 - How to add superscript in option tag select dropdown 1st 2nd 3rd 4th 5th. 1ST I want S captial letters but i tried s will converted to small case Puppeteer 打开下拉菜单,然后单击 [1st 2nd 3rd…] 选项 - Puppeteer Open Dropdown Menu and then click on [1st 2nd 3rd…] option 根据第一个选择设置第二个选择的选项 - Set option of the 2nd Select based on the 1st Select 如何在第一个选择上禁用(或更改颜色)第二个下拉选项? - How to disable (or change color) of 2nd drop down option depenging on 1st selection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM