English 中文(简体)
How can I operate on an DOM element cloned with jQuery?
原标题:
  • 时间:2009-11-13 04:41:08
  •  标签:
  • jquery
  • clone

I have a form that I m trying to duplicate using jQuery. I m using the clone() method, which returns the cloned object (DOM element). I need to then select elements of the cloned form and manipulate them. Is this possible? If so, how?
I ve tried stuff like:

var clonedForm = $("#myForm").clone();
clonedForm.$(".inputField").val();

But (unsurprisingly) the second line doesn t work. Any help would be appreciated, thanks.

最佳回答

I think

$(clonedForm).find( .inputField ).val()
问题回答

If you want to change the Id of the element you are cloning, try this

var clonedForm = $("#myForm").clone();

clonedForm.attr( { id:  new-id  } );

Well first what you get from the clone method is a piece of DOM that needs to be attached somewhere to be visible (it s not obvious from your code snippet if that s the case). Second - if you are using same IDs fir your elements you out of luck since findElementBYID will return you fisrt element it will find. You probably need go through your clone object ans change ID values. And then your syntax is also wrong in the second line as been pointed out

Some might find this more elegant

$(".inputField", clonedForm).val();

the second parameter specifies the context of the css selector.





相关问题
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: &...

热门标签