使用 jQuery 克隆表单
我有以下形式,
<div id="div1" style="margin-bottom:4px;" class="clon">
<span id ="display_input1">
Answer:<select name="Answer1" id="Answer1">
<option value='0'>Select Answer</option>
<option value='1'>Excellent</option>
option value='2'>Very Good</option>
</select>
</span>
New Answer<input type="checkbox" id="Enable1" name="Enable1" value="1" onclick=display_inpt(this) />
<?
function dropdown($starting, $factor, $ending)
{
echo "Point<select name=\"Mark1\" id=\"Mark1\">";
echo "<option value=\"\">Select Point</option>";
$i=$starting;
while($i >= $ending)
{
echo "<option value=\"$i\">$i</option>";
$i=$i-$factor;
}
echo "</select>";
}
dropdown(10, .5, -10);
?>
</div>
<input type="button" id="Add" value="Add Another Option" />
我想在每次单击“添加另一个选项”按钮时克隆整个 div,这样 rasult 就会像
<div id="div2" style="margin-bottom:4px;" class="clon">
<span id ="display_input2">
Answer:<select name="Answer2" id="Answer2">
这样。我尝试使用以下 jquery 代码来实现此目的,
$(document).ready(function() {
$('#Add').click(function() {
var num = $('.clon').length;
var newNum = new Number(num + 1);
var newElem = $('#div' + num).clone().attr('id', 'div' + newNum);
newElem.children(':eq(0)').attr('id', 'display_input' + newNum);
newElem.children(':eq(1)').attr('id', 'Answer' + newNum).attr('name', 'Answer' + newNum);
newElem.children(':eq(2)').attr('id', 'Enable' + newNum).attr('name', 'Enable' + newNum).attr('value', newNum);
newElem.children(':eq(3)').attr('id', 'Mark' + newNum).attr('name', 'Mark' + newNum);
但这是不正确的,那么我应该如何编写 jquery 代码来实现此目的?
I have the following form
<div id="div1" style="margin-bottom:4px;" class="clon">
<span id ="display_input1">
Answer:<select name="Answer1" id="Answer1">
<option value='0'>Select Answer</option>
<option value='1'>Excellent</option>
option value='2'>Very Good</option>
</select>
</span>
New Answer<input type="checkbox" id="Enable1" name="Enable1" value="1" onclick=display_inpt(this) />
<?
function dropdown($starting, $factor, $ending)
{
echo "Point<select name=\"Mark1\" id=\"Mark1\">";
echo "<option value=\"\">Select Point</option>";
$i=$starting;
while($i >= $ending)
{
echo "<option value=\"$i\">$i</option>";
$i=$i-$factor;
}
echo "</select>";
}
dropdown(10, .5, -10);
?>
</div>
<input type="button" id="Add" value="Add Another Option" />
I want to clone entire div on each click on the button "Add Another Option" such that rasult will be like
<div id="div2" style="margin-bottom:4px;" class="clon">
<span id ="display_input2">
Answer:<select name="Answer2" id="Answer2">
and so on. I tried with the following jquery code to achieve this
$(document).ready(function() {
$('#Add').click(function() {
var num = $('.clon').length;
var newNum = new Number(num + 1);
var newElem = $('#div' + num).clone().attr('id', 'div' + newNum);
newElem.children(':eq(0)').attr('id', 'display_input' + newNum);
newElem.children(':eq(1)').attr('id', 'Answer' + newNum).attr('name', 'Answer' + newNum);
newElem.children(':eq(2)').attr('id', 'Enable' + newNum).attr('name', 'Enable' + newNum).attr('value', newNum);
newElem.children(':eq(3)').attr('id', 'Mark' + newNum).attr('name', 'Mark' + newNum);
but this is not correct, so How should I write jquery code to achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查这个小提琴: http://jsfiddle.net/techfoobar/JwwkT/
id 和的基础名称重命名是通过以下方式完成的:
可能的增强功能是将选择器、新索引和父容器作为参数传递给上述函数。例如:
Check this fiddle: http://jsfiddle.net/techfoobar/JwwkT/
The basis of the id and name renaming is done via:
A possible enhancement would be passing the selector, the new index and the parent container as arguments to the above function. For ex:
下面的 jsfiddle 示例可以满足您的需求: http://jsfiddle.net/nLJTP/
我写了一个简短的代码片段,它增加所有元素的 id 和名称,这些元素具有“increaseNumbers”类:
The following jsfiddle example does what you want: http://jsfiddle.net/nLJTP/
I have written a short snippet, which increase the id and name of all elements, which have the class "increaseNumbers":