【发布时间】:2019-09-13 06:50:41
【问题描述】:
ajax 成功后,数据按行显示,但如果我想在那时添加新行,则添加按钮不显示。
我的代码:
<div class="row">
<div class="col-12">
<h3>Add Video Detail</h3>
<form id="insertvideo" method="post">
<table id="addrow" width="100%">
<td><input type="button" class="btn btn-success addButton col-3 offset-1" value="add"/></td>
<tr class="clonetr">
<td>Video Title<input type="text" id="videotitle" name="videotitle[]" class="form-control"></td>
<td>Video description<input type="text" id="videodesc" name="videodesc[]" class="form-control"></td>
<td>Video Links<input type="text" id="videolink" name="videolink[]" class="form-control"></td>
<td><input type="button" class="btn btn-danger deleteButton" value="delete"/></td>
</tr>
</table>
</form>
</div>
</div>
<div class="col-sm-6" style="margin-top: 13px; margin-left: 400px;">
<button type="submit" id="btn-update" value="Submit" class="btn btn-primary col-3 offset-1">Save</button>
</div>
<script>
$(function(){
$(".addButton").click(function(){
$('.clonetr:last').clone(true).appendTo("#addrow");
});
$(".deleteButton").click(function(){
if($('.deleteButton').length > 1)
$(this).closest("tr").remove();
else
alert('Atleast one required.');
});
});
var mainCatId = $(this).val();
$.ajax({
url: "<?php echo base_url();?>admin_controller/WebsiteContentController/fetchSubDetail",
type: "POST",
data: {mainCatId:mainCatId},
dataType:'html',
success: function(response)
{
$('#addrow').html(response);
console.log(response);
}
});
</script>
我不知道我的代码哪里错了。 我想添加一个新行,但添加按钮显示以添加一个新行。
【问题讨论】:
-
您的 html 错误,您不应该在
<table>中直接包含<td>,它应该是<table><tbody><tr><td>或<table><tr><td> -
在您的回复中,您使用
$('#addrow').html(response);,这意味着您替换了表格的全部内容。这就是您的添加按钮被删除的原因。 -
@CarstenLøvboAndersen 如何显示
add按钮
标签: javascript php jquery ajax codeigniter