jquery - 从选择中删除一个选项并将其添加到另一个选择中

标签 jquery

好的,我的这段代码过去工作得很好。我最近将 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/

相关文章:

jquery - CSS 背景覆盖固定

javascript - 将时间格式转换为字符串并反转

javascript - $tree.tree 不是函数

jquery - 使用 Bootstrap 3 和 ID 远程加载选项卡

javascript - 使用 Ajax 加载 html 后执行代码

javascript - Jquery/Javascript 将 $(this) 链接到不同 div 中的元素?

xml - 如何在Jquery中访问xml文本节点

javascript - 触发 Google 跟踪代码管理器 jQuery

jquery - 显示 div IF 图像具有 ALT 属性

javascript - 单击按钮将 HTML div 内容导出为 pdf