从select中删除一个选项并将其添加到另一个select中

时间:2022-08-24 11:13:42

Okay, so I've had this code that used to work just fine. I recently upgraded my jQuery from 1.4.4 to 1.5.2, and apparently this has quit working. However, I've tried the code with 1.4.4. and 1.3.2 and it won't work there, either.

好的,我已经有了这样的代码,它以前工作得很好。我最近将jQuery从1.4.4升级到1.5.2,显然这已经不起作用了。但是,我已经尝试了1.4.4的代码。1。3。2也不行。

This did work. I can't figure out why it isn't. Any help?

这是工作。我不知道为什么不是。任何帮助吗?

Edit: start and end are arguments, with the text of the select element's ID.

编辑:开始和结束都是参数,带有select元素ID的文本。


var selectedIndex = document.getElementById(start).selectedIndex; // get the selected index from the correct select box

if (selectedIndex != -1) {      // if something is selected, do the following:
       var selectedElement = document.getElementById(start).options[selectedIndex]; // get the element that's selected

       if (selectedIndex == (document.getElementById(start).options.length - 1) && selectedIndex != 0) {
           selectedIndex--; // if we're at the bottom of the list, set our selectedIndex variable to the one right before it
       }

       $("#" + start).remove(selectedElement);  // remove the selected element from the start side          
       $("#" + end).append(selectedElement);        // and add it to the end of the ending side
}

Here's an example of an option I want to move.
<option sortable="yes" datatype="string" value="foo" type="arbitrary">Foo</option>

这里有一个我想要移动的选项的例子。

The issue I'm getting is apparently within jQuery itself - using the full version,
expr.replace is not a function [Break On This Error] expr = expr.replace( /\=\s*([^'"]])\s]/g, "='$1']" ); [jquery-latest.debug.js, line 4540]
The error happens when I hit the $.remove portion of the code.

Thanks.

我得到的问题显然是jQuery本身的问题——使用完整的版本expr。替换不是一个函数[中断这个错误]expr = expr。替换(/ \ = \ s *([^”“]])\ s)/ g,”= ' $ 1 ')");[jquery-latest.debug.js,第4540行]当我点击$时发生错误。删除部分代码。谢谢。

3 个解决方案

#1


4  

You are getting this error because .remove() takes a string selector, and if none is supplied, removes those of the parent object. Try

您会得到这个错误,因为.remove()接受一个字符串选择器,如果没有提供,则删除父对象的选择器。试一试

$(selectedElement).remove();

#2


2  

Is there a reason you're using the getElementById calls? Let jQuery do the work for you... Instead of using:

使用getElementById调用有什么原因吗?让jQuery为您工作……而不是使用:

$("#" + start).remove(selectedElement);  // remove the selected element from the start side          
$("#" + end).append(selectedElement);        // and add it to the end of the ending side

}

}

try using:

尝试使用:

$("#"+start+" option:selected").appendTo("#" + end);

It combines the remove/append operations into one, and may solve your problem.

它将删除/追加操作组合为一个操作,可以解决您的问题。

#3


0  

Unfortunately, <option> is not ordinary element so it might cause such problems.

不幸的是,

See the accepted answer on this question for code that should work.

请参阅这个问题的公认答案,以获得应该有效的代码。

#1


4  

You are getting this error because .remove() takes a string selector, and if none is supplied, removes those of the parent object. Try

您会得到这个错误,因为.remove()接受一个字符串选择器,如果没有提供,则删除父对象的选择器。试一试

$(selectedElement).remove();

#2


2  

Is there a reason you're using the getElementById calls? Let jQuery do the work for you... Instead of using:

使用getElementById调用有什么原因吗?让jQuery为您工作……而不是使用:

$("#" + start).remove(selectedElement);  // remove the selected element from the start side          
$("#" + end).append(selectedElement);        // and add it to the end of the ending side

}

}

try using:

尝试使用:

$("#"+start+" option:selected").appendTo("#" + end);

It combines the remove/append operations into one, and may solve your problem.

它将删除/追加操作组合为一个操作,可以解决您的问题。

#3


0  

Unfortunately, <option> is not ordinary element so it might cause such problems.

不幸的是,

See the accepted answer on this question for code that should work.

请参阅这个问题的公认答案,以获得应该有效的代码。