简体   繁体   English

jQuery从其他DropDown删除A DropDown中的选定选项,停止工作并挂起

[英]JQuery to Remove a Selected Option in A DropDown from other DropDowns , stops working and hangs

I am using the following javascript to remove a option selected in DropDown A , from DropDown B. 我正在使用以下JavaScript从DropDown B中删除在DropDown A中选择的选项。

$('#A').bind('keypress', function(e){ 

      if ( e.which == 52 ){
        var selectedItems  = $('#A').val();
        var $list = $("#B"),
            toRemove = $(),
            selectedItems = $("#A").val();
        for ( var i = 0 ; i < selectedItems.length; i++) {
          toRemove = toRemove.add($list.find('option[value="' + selectedItems[i] + '"]'));
        }
        toRemove.remove();
      }
});

The option is removed from DropDown B , but , the javascript hangs out when I press the key. 该选项已从DropDown B中删除,但是,当我按键时,javascript会挂出。 Please help. 请帮忙。

Is there some other jquery ? 还有其他的jQuery吗?

I can't explain why everything is hanging, but I think there are some things that could be improved in your code. 我无法解释为什么一切都挂起了,但是我认为您的代码中有一些可以改进的地方。

If you have a "dropdown" then the select element will be single-select, which means the .val() method will be returning a string rather than an array (it only returns an array in the case of a <select multiple="multiple"> element), and if it's returning a string you don't need the for loop because - well, there's nothing to loop through. 如果您有一个“下拉列表”,则select元素将是单选的,这意味着.val()方法将返回字符串而不是数组(仅在<select multiple="multiple">的情况下返回数组) <select multiple="multiple"> )元素),并且如果它返回一个字符串,则不需要for循环,因为-嗯,没有循环可做。 If that is true the simplified version of your code would be this: 如果是这样,则您的代码的简化版本应为:

$('#A').bind('keypress', function(e){
    if ( e.which === 52 ){
       $('#B option[value="' + $(this).val() + '"]').remove();
    }
});

If there is no matching option in #B for the selected item then nothing will happen. 如果#B中没有与所选项目匹配的选项,则不会发生任何事情。

I don't understand the significance of key code 52, but I've left that in. 我不了解键控代码52的重要性,但我没有提及。

暂无
暂无

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

相关问题 具有相同选项的多个下拉菜单:如何从其他下拉菜单中删除一个下拉列表的当前选定项目? - Multiple dropdowns with same options: how to remove currently selected item of one dropdown from other dropdowns? 下拉选项从其他下拉菜单中删除 - Dropdown Options remove from other dropdowns 在特定下拉列表中选择某个选项后,从所有其他选定的多个下拉列表中删除一个选项 - Removing an option from all other chosen multiple dropdowns, when selected on a particular dropdown 删除在一个下拉列表字段中选择的选项,并且不显示在该行不存在的其他下拉列表中 - remove the option selected in one dropdown list field and don't show in other dropdown list present in that row not working- jquery 当我从下拉列表中选择一个选项时,jQuery typeahead停止工作 - jQuery typeahead stops working when I select an option from the dropdown 使用jquery&#39;this&#39;从下拉列表中返回所选选项,但它返回页面上的所有下拉列表 - Using jquery 'this' to return selected option from dropdown, but it returns all dropdowns on page 从下拉选项中删除“选定” - Remove “selected” from an option of a dropdown 根据彼此选择的选项从4个选择下拉列表中删除选项? (允许jquery) - Remove options from 4 select dropdowns based on selected options in each other? (jquery allowed) 从下拉列表中删除选择的选项 - Remove selected option from dropdown list 在上一个下拉列表中选择某个选项后,将其删除 - Remove an option once it is selected in previous dropdowns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM