[英]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? 谁能帮我这个?
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/hide
该option
。 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.