jQuery的克隆选择不保留价值 [英] jquery clone select doesnt keep value
问题描述
我试图克隆一个小集,然后提交输入内容并使用序列化进行选择.它工作正常,但是select不能保持其价值.我尝试了几种发现的方法,但是似乎没有任何效果.这是我克隆和设置当前数据的方式.
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.
克隆时如何保持select的值?
How can I keep the value of select when cloning?
$('body').append('<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();
推荐答案
option
元素通过selected
javascript属性保持当前的选择(不要与selected
属性混淆,该属性对应于默认选择).
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).
由于jQuery的clone
不会克隆当前所选内容( http://bugs.jquery.com/ticket/1294 ),则必须手动进行:
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());
这篇关于jQuery的克隆选择不保留价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!