简体   繁体   English

选择其他项目时,在选择框中添加和删除选项

[英]add and remove options from select box when other item selected

I have used the code (below) which was featured on jsfiddle. 我使用了jsfiddle中精选的代码(如下)。 The problem i have is that if i change the option value to be different from the text value, it then adds the value back into the text field on change. 我的问题是,如果我将选项值更改为与文本值不同,那么它将在更改时将值添加回文本字段中。

jQuery(function(){
            jQuery('.selbox').change(function(){
            var val = jQuery(this).val();

            var sel = jQuery(this);

            if(val != "")
            {
            if(sel.data("selected"))
            {   
            var oldval = sel.data("selected");


            sel
           .siblings('select')
           .append(jQuery('<option/>').attr("value", oldval).text(oldval));
           }

           sel
            .data("selected", val)
            .siblings('select')
            .children('option[value=' + val + ']')

            .remove();
          }
          else if(val == "")
          {
          if(sel.data("selected"))
          {   
           var oldval = sel.data("selected");


           sel
           .removeData("selected")
           .siblings('select')
           .append(jQuery('<option/>').attr("value", oldval).text(oldval));
           }
           }            
           });
           });

I am using code 我正在使用代码

<option value="1">text here</option>

Please see here for the original code http://jsfiddle.net/BUuZv/2/ 请在此处查看原始代码http://jsfiddle.net/BUuZv/2/

Instead of removing them and adding them, you could simply hide/show them like this fiddle 除了删除并添加它们,您还可以像这样摆弄或 hide/show它们

$(function() {
    $('.selbox').change(function() {
        var val = $(this).val();
        var sel = $(this);

        if (val != "") {
            if (sel.data("selected")) {
                var oldval = sel.data("selected");
                sel.siblings('select').find('option[value="' + oldval + '"]').show()
            }

            sel.data("selected", val).siblings('select').children('option[value=' + val + ']').hide();
        }
        else {
            if (sel.data("selected")) {
                var oldval = sel.data("selected");
                sel.removeData("selected").siblings('select').find('option[value="' + oldval + '"]').show()
            }
        }
    });
});

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

相关问题 选择项目时,AngularJS选择框选项会消失 - AngularJS select box options disappear when an item is selected 从另一个多选2框中删除所选项目时,从多个select2框中删除所选项目 - Remove a selected item from multiple select2 box on removal of selected item from another multiple select 2 box 从选择框中删除选项(如果已在其他位置选择) - Remove options from select box if it is already selected in onother 如何使用Javascript Prototype从多个选择框中删除所选选项? - How to remove selected options from multiple select box with Javascript Prototype? 当选择另一个框时,从选择框中删除“禁用”属性 - Remove 'disable' property from select box when another box is selected 从选择框中动态删除在其他选择框中选择的选项, - Deleting dynamically options from select box that were selected in other select box, Javascript SELECT BOX选项取决于所选的其他SELECT BOX选项 - Javascript SELECT BOX options dependent on other SELECT BOX option selected 从所选项目中删除和删除项目时,如何在反应选择中捕获添加和删除事件? - How to catch add and remove event in react-select when andding and removing item from selected? 从 Select 盒子中取出物品 - Remove Item from Select Box 如何添加和删除带有选项的选择框? - How to add and remove a select box with options?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM