好的,我的这段代码过去工作得很好。我最近将 jQuery 从 1.4.4 升级到 1.5.2,显然这已经停止工作了。不过,我已经尝试过 1.4.4 的代码。和 1.3.2 也不起作用。
这确实有效。我不明白为什么不是。有什么帮助吗?
编辑:开始和结束是参数,文本为 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
}
这是我想要移动的选项的示例。
<code><option sortable="yes" datatype="string" value="foo" type="arbitrary">Foo</option></code>
我遇到的问题显然是在 jQuery 本身内部 - 使用完整版本,
expr.replace is not a function
[Break On This Error] expr = expr.replace( /\=\s*([^'"]]<em>)\s</em>]/g, "='$1']" ); [jquery-latest.debug.js, line 4540]
当我点击$.remove
时发生错误部分代码。
谢谢。
最佳答案
您收到此错误是因为 .remove()
采用字符串选择器,如果没有提供,则删除父对象的选择器。尝试一下
$(selectedElement).remove();
关于jquery - 从选择中删除一个选项并将其添加到另一个选择中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5858843/