简体   繁体   English

根据“使用Java脚本的第一次”中的选择从第二个下拉菜单中删除项目

[英]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>

You can use such code : jsFiddle . 您可以使用以下代码: jsFiddle

Basically, you first bind the change event to each list and when you change the value you hide these elements in all lists after... 基本上,您首先将change事件绑定到每个列表,并且当您更改值时,在...之后将这些元素隐藏在所有列表中。

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.

相关问题 根据第一个下拉选择jquery显示第二个下拉选项 - Show second dropdown options based on first dropdown selection jquery 如何基于第一选择填充第二选择菜单,如何基于第二选择填充第三选择菜单-使用Javascript - How to populate a second select menu based on the first selection and third based on second - Using Javascript 基于第一个选择的Ajax方法填充第二个动态下拉菜单 - Ajax approach to populating a second dynamic dropdown based on the selection in the first 如何根据用户从下拉列表中的选择访问数组元素的其他字段,以及如何使用 javascript 删除重复项 - How to access other fields of array elements based on user selection from dropdown list and also how to remove the duplicates using javascript 根据第一个下拉列表中的值填写第二个下拉列表 - Fill second dropdown based on value from the first 无法在Mozilla Firefox中使用javascript从下拉列表中删除项目 - Unable to remove an item from dropdown list using javascript in Mozilla Firefox 使用Java脚本基于下拉列表显示表 - Show Table Based on Dropdown selection Using Javascript 如何基于第一个下拉列表选择获取动态生成的两个下拉列表的第二个下拉列表的ID? - How to get id of second dropdown of dynamically generated two dropdown based on first dropdown selection? 根据使用数据库选择的第一个下拉列表来更新秒下拉列表 - Update seconds dropdown based on the selection of the first dropdown using the database 使用 React JS 上的函数根据第一个下拉选择填充下拉列表 - Populating a dropdown based on the first dropdown selection using funcion on React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM