我已经输入并添加了一个按钮。单击添加按钮后,输入值应保存表格行,我需要一个删除按钮。我需要删除个人的那个值。我需要保存很多值。我尝试在 jquery 中获得表中的附加值,但无法删除该值。下面是我的代码。提前致谢。
<div class="col-sm-6 col-md-5">
<div class="form-group">
<label class="control-label" for="supplyingLocation">Supplying to Location <span class="text-danger">*</span></label>
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter Supplying Location Name" name="supplyingLocation" id="supplyingLocation" required="required">
<div class="input-group-btn">
<button class="btn btn-primary add-supplying-location-row" type="button">Add Location </button>
</div>
</div>
</div>
</div>
<div class="clear"></div>
<div class="col-sm-5">
<div class="listing-table">
<div class="table-responsive sup-loc-table">
<table class="table table-bordered">
<thead>
<tr>
<th>Supplying to Location</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(".add-supplying-location-row").click(function(){
var supname = $("#supplyingLocation").val();
$('#supplyingLocation').val('');
var supmarkup = "<tr><td>" + supname + "</td><td><input type='button' name='supplyingLocRecord' value='Delete' id='deletesupLoc'></td></tr>";
$(".sup-loc-table table tbody").append(supmarkup);
});
// Find and remove selected table rows
$("#deletesupLoc").click(function(){
$(".sup-loc-table table tbody").find('input[name="supplyingLocRecord"]').each(function(){
{
$(this).parents("tr").remove();
}
});
});
});
</script>
最佳答案
您非常接近答案。请注意以下事项:
首先:您绝对不应该有重复的 ID。它可能有效,但它在语义上是不正确的,你不应该这样做。您正在添加同一行 onclick,因此您应该给它相同的类
其次:名称属性也是如此。如果你想捕获相同类型的多个数据,你应该使用
supplyingLocRecord[]
.它将所有元素捕获到单个数组中。<input type='button' name='supplyingLocRecord[]' value='Delete' class='deletesupLoc'>
第三:
click()
您正在使用的绑定(bind)称为“直接”绑定(bind),它只会将处理程序附加到已经存在的元素。它不会绑定(bind)到将来创建的元素。为此,您必须使用on()
创建一个“委托(delegate)”绑定(bind)。 .$(document).on('click','.deletesupLoc',function(){ $(this).parents("tr").remove(); });
关于jquery - 如何使用 jquery 添加和删除输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58095916/