[英]Remove item from second dropdown based on selection in First using Javascript
I have four dropdown, and I am manually filling them. 我有四个下拉菜单,我正在手动填充它们。
Now I want to add a javacsript that when I select first dropdown option, then in the second third fourth dropdowns, that item or option can be removed. 现在,我想添加一个javacsript,当我选择第一个下拉选项时,然后在第二个第三个,第四个下拉菜单中,可以删除该项目或选项。
And same flow goes for second third and fourth and so on. 同样的流向第二,第三和第四,依此类推。
I am giving my code but till now, it is not working fine. 我正在提供代码,但到目前为止,它仍然无法正常工作。
I am only tring for the first ladder, that is when option in first is selected then item removed from second, third and fourth dropdowns. 我只是在尝试第一个阶梯,即选择了第一个阶梯中的选项,然后从第二,第三和第四下拉菜单中删除了项目。
function RemoveItems(){
var List1 = document.getElementById("ddlSortField");
var sortList1 = List1.options[List1.selectedIndex].text;
var List2 = document.getElementById("ddlSortField2");
var sortList2 = List2.options[List2.selectedIndex].text;
List2.options.remove(sortList1);
var List3 = document.getElementById("ddlSortField3");
var sortList3 = List3.options[List3.selectedIndex].text;
List3.options.remove(sortList2);
var List4 = document.getElementById("ddlSortField4");
var sortList4 = List4.options[List4.selectedIndex].text;
List4.options.remove(sortList3);
}
Use jQuery to remove option 使用jQuery删除选项
$(document).ready(function(){
$('#ddlSortField').change(function(){
var index = $("#ddlSortField option:selected").val();
$('#ddlSortField2 option:eq(' + index + ')').remove();
$('#ddlSortField3 option:eq(' + index + ')').remove();
$('#ddlSortField4 option:eq(' + index + ')').remove();
});
});
Note in your html your option value must be same like this: 请注意,在您的html中,您的选项值必须像这样:
<select id="ddlSortField">
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
<select id="ddlSortField1">
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
I've made a slightly different one than @Muhammad Omair's, this one is a bit more dynamic. 我制作的内容与@Muhammad Omair的稍有不同,该内容更具动态性。 Note that this is jQuery
请注意,这是jQuery
var removeSelection = function(select) {
$('select').filter(':not(#' + select.attr('id') + ')').each(function() {
var index = select.find(':selected').index();
$(this).find('option:eq(' + index + ')').remove();
});
};
$(function() {
$('select').change(function() {
removeSelection($(this));
});
});
And here's a jsfiddle of it http://jsfiddle.net/cA3F9/ 这是它的jsfiddle http://jsfiddle.net/cA3F9/
In your code: 在您的代码中:
> function RemoveItems(){
Variable names starting with a capital letter are, by convention, reserved for constructors, so: 按照惯例,以大写字母开头的变量名称保留给构造函数使用,因此:
function removeItems() {
> var List1 = document.getElementById("ddlSortField");
> var sortList1 = List1.options[List1.selectedIndex].text;
So sortList1 will be a string. 因此sortList1将是一个字符串。
> var List2 = document.getElementById("ddlSortField2");
> var sortList2 = List2.options[List2.selectedIndex].text;
> List2.options.remove(sortList1);
The remove method of the options collection takes a single parameter that is an index of one of the options. 选项集合的remove方法采用单个参数,该参数是选项之一的索引。 You have not shown what the value of sortList1 nor how many options List2 has.
您尚未显示sortList1的值或List2有多少个选项。 Note that the options collection is live, so if you remove an option, the indexes of other options may be adjusted so that they are contiguous from 0 to
options.length - 1
. 请注意,选项集合是实时的,因此,如果删除选项,则可能会调整其他选项的索引,以使它们从0到
options.length - 1
连续。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.