English 中文(简体)
将选择从一个选择中删除,并将其添加到另一个选择中
原标题:Removing an option from a select and adding it to another
  • 时间:2011-05-02 15:09:10
  •  标签:
  • jquery

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.

确实如此。 我可以说明为什么如此。 任何帮助?

Edit:开始和结束是论点,select要素 s 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.

最佳回答

由于<代码>remove(<>>>> 采用扼杀性选择器,如果供应不,则删除母物体。 提 出

$(selectedElement).remove();
问题回答

您是否有理由重新使用没收财产。 电话? 让我们做你们的工作...... 不使用:

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

iii

try using:

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

它把搬走/申请行动结合起来,并可能解决你的问题。

不幸的是,<代码><option>不是普通内容,因此可能会造成这些问题。

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





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签