这里是我的第一个问题,但在过去的一年里,stackoverflow对我来说是一个非常有价值的资源。
我正在构建一个高级表单。我需要用户有能力添加子组。我使用的是jquery (1.5.1 --我想我应该更新。)克隆对象。我的问题是我也需要它来更新属性。
我想知道是否存在一种我找不到的方法来做这件事,或者我是否应该自己写这篇文章。这似乎是一项简单的任务,计算属性并添加一个。
我已经提供了一些代码的简明示例。
<form class="clause">
<input type="radio" id="radio-1" name="radio" checked="checked" />
<label for="radio-1">And</label>
<input type="radio" id="radio-2" name="radio" />
<label for="radio-2">Or</label>
<input type="radio" id="radio-3" name="radio" />
<label for="radio-3">Not</label>
<button class="ag">Add SubGroup</button>
</form>
<!-- yes, this is in an external file -->
<script type="text/javascript">
$(function() {
$('.ag').click(function() {
$('.clause').clone(true).insertAfter($('.clause:last'));
});
</script>这很好地复制了表单,并在其自身之后插入了一个副本。
显然,单选按钮属性不会更新。因此,如果在一个子组中勾选了某个单选按钮,则该单选按钮将应用于所有子组。我不希望这种情况发生。
是否存在用于此目的的函数?如果没有的话,这不是问题,但我正在努力不在这个项目中重新发明任何轮子。
编辑:
为了更好地说明这个问题,我在下面提供了更多代码。详细说明我想要的结果
<form class="clause">
<input type="radio" id="radio-1" name="radio" checked="checked" />
<label for="radio-1">And</label>
<input type="radio" id="radio-2" name="radio" />
<label for="radio-2">Or</label>
<input type="radio" id="radio-3" name="radio" />
<label for="radio-3">Not</label>
<button class="ag">Add SubGroup</button>
</form>
<form class="clause">
<input type="radio" id="radio-4" name="radio" checked="checked" />
<label for="radio-4">And</label>
<input type="radio" id="radio-5" name="radio" />
<label for="radio-5">Or</label>
<input type="radio" id="radio-6" name="radio" />
<label for="radio-6">Not</label>
<button class="ag">Add SubGroup</button>
</form>
<!-- yes, this is in an external file -->
<script type="text/javascript">
$(function() {
$('.ag').click(function() {
$('.clause:last').clone(true).insertAfter($('.clause:last'));
});
</script>(注意:这是我正在尝试的内容的高度缩小的版本,如果我适当地缩放了它,请原谅。)
谢谢,马特
发布于 2011-05-05 01:35:40
$('.ag').click(function() {
// I would advise using :last so you don't exponentially grow your form
var clone = $('.clause:last').clone(true);
// now find all the radios and reset them
$('input[type=radio]',clone).removeAttr('checked');
// now add it
clone.insertAfter($('.clause:last'));
});使用选择器(作用域)的第二个参数,并在克隆中进行搜索以在附加它之前重置字段/属性。
https://stackoverflow.com/questions/5887328
复制相似问题