我的代码的 jsFiddle:http: //jsfiddle.net/8Vcyu/
我设置了一个表单,用户可以在其中输入 1 到 10 行信息。如果用户添加第 10 行 jquery 删除“添加行”按钮。如果删除第 10 行,则添加按钮会返回。这一切都很好,但是当“添加行”按钮被添加回页面时,它不再起作用 - 没有添加新行。非常感谢任何帮助,这个问题让我很困惑。
HTML
<form name="input" action="/engine/preview.php" enctype="multipart/form-data" id="customizeForm" method="post">
<div id="customize_container">
<div id="customize_right">
<table class="customize_table">
<tr class="tr_clone">
<td>
<input type="text" name="title[]" value="NAME" maxlength="12" />
</td>
<td>
<input type="text" name="entry[]" value="Your Name" maxlength="20" />
</td>
<td>
<a href="#" class="closeRow"></a>
</td>
</tr>
<tr class="tr_clone">
<td>
<input type="text" name="title[]" value="NAME" maxlength="12" />
</td>
<td>
<input type="text" name="entry[]" value="Your NAME" maxlength="20" />
</td>
<td>
<a href="#" class="closeRow">remove</a>
</td>
</tr>
<tr class="tr_clone">
<td>
<input type="text" name="title[]" value="NAME" maxlength="12" />
</td>
<td>
<input type="text" name="entry[]" value="Your ID" maxlength="20" />
</td>
<td>
<a href="#" class="closeRow">remove</a>
</td>
</tr>
<tr class="tr_clone">
<td>
<input type="text" name="title[]" value="NAME" maxlength="12" />
</td>
<td>
<input type="text" name="entry[]" value="Your Account Name" maxlength="20" />
</td>
<td>
<a href="#" class="closeRow">remove</a>
</td>
</tr>
<tr class="tr_clone">
<td>
<input type="text" name="title[]" value="LABEL" maxlength="12" />
</td>
<td>
<input type="text" name="entry[]" value="Information" maxlength="20" />
</td>
<td>
<a href="#" class="closeRow">remove</a>
</td>
</tr>
</table>
<div id="add_row_button">
<input type="button" name="add" value="Add" class="tr_clone_add" />
</div>
</div>
<div class="clear"></div>
<input type="submit" value="Preview Your Card" class="preview_cards" />
</div>
</form>
JS
function countRows() {
return $(".customize_table tr").length;
}
$(".closeRow").on('click', function() {
$(this).closest('tr').remove();
var $rows = countRows();
if($rows == 9) {
$("#add_row_button").append("<input type='button' name='add' value='Add' class='tr_clone_ad' />");
}
});
$("input.tr_clone_add").on('click', function() {
var $rows = countRows();
if($rows <= 9) {
var $tr = $("table.customize_table tr:last-child");
var $clone = $tr.clone( true );
$tr.after($clone);
$rows = countRows();
if($rows == 10) {
$(".tr_clone_add").remove()
}
}
});
$(document).ready(function() {
$("#customizeForm").ajaxForm({
success: function(responseText){
$.fancybox({
'content' : responseText,
'minWidth' : 800,
'minHeight' : 600,
'scrolling' : 'no',
'type' : 'iframe',
});
}
});
});