简体   繁体   English

如何基于html中的另一个选择选项来操纵选择选项?

[英]How to manipulate a select options based on another select option in html?

I have two select tags (both are same) : 我有两个选择标签(都是一样的):

<select class="form-control" name='end'>
    <option selected disabled hidden value="">end</option>

    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
</select>

Here, if I select one option from first dropdown , then in second dropdown all option up to the selected one should not be shown. 在这里,如果我从第一个下拉列表中选择一个选项,则在第二个下拉列表中,直到所选的所有选项都不会显示。 For example, If I select 5 in first dropdown then second dropdown should show only 6-11. 例如,如果我在第一个下拉列表中选择5,则第二个下拉列表应仅显示6-11。 Can anyone help me with this? 谁能帮我这个?

JSFIDDLE JSFIDDLE

You need to add an onchange event listener, and use the value of the first select to loop through the second select and hide the options. 您需要添加一个onchange事件侦听器,并使用第一个选择的值在第二个选择中循环并隐藏选项。

Using jQuery: 使用jQuery:

var max = 0;
var options = $("select[name='end']").find('option');

$("select[name='start']").on('change', function() {

    options.each(function(){
        $(this).show();
    });

    max = parseInt( $(this).val() );

    for (i=0; i < max; i++) {
        $(options[i]).hide();
    }

});

give ids to your selects, for example: #firstSelect and #secondSelect I made this solution using jquery because it is simpler than with plain javascript. 为您的选择提供ID,例如: #firstSelect#secondSelect我使用jquery做出了此解决方案,因为它比使用普通javascript更为简单。 Try this: 尝试这个:

 $("#firstSelect").change(function () {
      var selectedOption = $("select option:selected").text();
      $("#secondSelect option").each(function() {
        var txtOption = $(this).text();
        if(parseInt(txtOption) <= parseInt(selectedOption)){ 
          $(this).remove();
        }
      });
});

If you want this to work more than once, so that when you change the first one again the second one changes again and adds or remove the options fresh from the begining: 如果您希望此操作不止一次,那么当您再次更改第一个时,第二个将再次更改,并从一开始就添加或删除新鲜的选项:

$("#firstSelect").change(function () {
    var selectedOption = $("select option:selected").text();
    $("#secondSelect").empty();
    for (var index = 2; index < 12; index++) {
        if(index >= parseInt(selectedOption)){
            $("#secondSelect").append(
                $('<option></option>').html(index)
            );
        }
    }
});

check the indexes in the for loop, these are hardcoded from 2 to 12. You could do that dinamically aswell. 检查for循环中的索引,这些索引从2到12进行硬编码。您也可以执行同样的操作。 But if there are always the options 1,2,3...9,10 in the first dropdown, this should work fine. 但是,如果在第一个下拉列表中始终有选项1,2,3 ... 9,10,则应该可以正常工作。

You need to associate a change event listener to the select dropdown with name start so that when each option is selected from this dropdown you can show/hide the option in end dropdown like this. 您需要将更改事件侦听器与名称为start的选择下拉列表关联,以便从该下拉列表中选择每个选项时,您可以像这样在end下拉列表中show/hideoption Also, make note of parseInt() which will compare the option value as a integer and give you the correct result. 另外,记下parseInt() ,它将把option值作为一个整数进行比较,并为您提供正确的结果。

 $('select[name="start"]').change(function(){ var selectedValue = parseInt($(this).val()); changeEndOptions(selectedValue); }); function changeEndOptions(startValue){ var $options = $('select[name="end"] option'); $options.each(function(){ var optionVal = parseInt($(this).val()); if(optionVal <= startValue){ $(this).hide(); } else { $(this).show(); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="form-control" name='start'> <option selected disabled hidden value="">start</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> <select class="form-control" name='end'> <option selected disabled hidden value="">end</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> <option>11</option> </select> 

暂无
暂无

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

相关问题 如何根据另一个选择结果更新 HTML 选择选项 - How to update an HTML select options based on another select result 如何限制一个选项 <select>根据另一个选项选择 - How to limit options in a <select> based on another option selection 根据另一个选定的选项更改 select 选项 - Change select options based on another selected option 如何根据表中的其他选择选项更改选项? - How to change options based on another select option in a table? 如何根据另一个select html元素的选项值使用jQuery遍历select html选项 - How to iterate through select html options using jQuery depending on option value from another select html element 如何根据另一个Ember.Select的选定选项通过计算属性填充Ember.Select选项 - how to populate Ember.Select options via computed property based on selected option of another Ember.Select 如何基于具有相同选项值的另一个选择元素禁用选择元素中的选项? - How to disable the options in select element based on another select element with same option values? HTML JAVASCRIPT或PHP如何使select中的选项依赖于另一个select中选择的选项 - HTML JAVASCRIPT or PHP how to make options in select dependent on option selected in another select 如何动态操纵选择选项 - How to manipulate select option dynamically 如何隐藏和显示另一个选择选项栏的选择选项 - how to hide and show select options for another select option bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM