如何逐个从div类中获取内容然后将其加载到数组中?然后我需要将这些逐个插入到其他div类中。 基本上,我有2个表单,其中一个是虚拟的,这个虚拟从CMS获取其内容。虚拟表单是隐藏的,而真实表单则显示,但最初是空的。 我需要使用jquery从表单中获取虚拟文本并将其插入到真实表单中。
这样的事情:
<form name="real" method="post" action="">
<input type="text" name="first" id="a"/>
<input type="text" name="second" id="b"/>
<input type="text" name="third" id="c"/>
<input type="text" name="fourth" id="d"/>
<input type="submit" value="submit"/>
</form>
<form name="extract" style="display:none;">
<div class="generic">data_1</div>
<div class="generic">data_2</div>
<div class="generic">data_3</div>
<div class="generic">data_4</div>
</form>
必须成为这样的东西:
<form name="real" method="post" action="">
data_1 <input type="text" name="first" id="a"/>
data_2 <input type="text" name="second" id="b"/>
data_3 <input type="text" name="third" id="c"/>
data_4 <input type="text" name="fourth" id="d"/>
<input type="submit" value="submit"/>
</form>
有办法做到这一点吗? 谢谢!
答案 0 :(得分:2)
有很多方法可以做到这一点。例如:
$('[name=extract] div').each(function(index){
$('[name=real] input:eq('+index+')').before($(this).text());
});
编辑:这是所用方法的api页面:
http://api.jquery.com/attribute-equals-selector/
http://api.jquery.com/each/
http://api.jquery.com/eq-selector/
http://api.jquery.com/before/
答案 1 :(得分:0)
您可能想查看jQuery DataLink Plugin
答案 2 :(得分:0)
我会提供这个版本:
$('.generic').each(
function(i){
$('input:text').eq(i).val($(this).text());
});
div.generic
:input[type=text]