开发者

jquery clone select doesnt keep value

I am trying to clone a feildset then submit the contents of inputs and selects using serialize. It is working properly however select doesn't keep its value. I have tried several methods I have found but nothing seems to work. Here is how I am cloning and setting the current data.

How can I keep the value of select when cloning?

 $('body').a开发者_如何学运维ppend('<form id="form-to-submit" style="visibility:hidden;"></form>');  
var fieldsetName = $this.parents('.fieldsetwrapper');  
$('#form-to-submit').html($(fieldsetName).clone());  
var data = $('#form-to-submit').serialize();  


The option element maintains its current selectedness with the selected javascript property (not to be confused with the selected attribute, which corresponds to default selectedness).

Since jQuery's clone doesn't clone the current selectedness (http://bugs.jquery.com/ticket/1294) , you'll have to do it manually:

$('#form-to-submit').html($(fieldsetName).clone());
$('#form-to-submit select').val($('.fieldsetwrapper select').val());


You should be able to set the value of the new select element to the value of the old one:

$('#form-to-submit select').val($('.fieldsetwrapper select').val());


In my case I was using

$('<form></form>').html($('.myDiv').clone())

and fixed the issue by adding another form to the markup

$('.myForm').serialize()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜