您好,我想创建一个动态按钮,尽管我对ajax或jquery不熟悉,所以请指导我,我已经在运行此动态按钮,但是我想要的是在添加其他表单后创建另一个动态按钮。所以这是我的代码。看到我的图片,添加表单后我无法添加另一个文件按钮
我在这里是表格的设计
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="text" name="name[]" class="form-control-file" id="exampleFormControlFile1">
<textarea class="form-control" name="areaName[]" rows="5" id="comment"></textarea>
<input type="file" name="fileName[]" class="form-control-file" id="exampleFormControlFile1"><br>
<!--I Want to add another File Button after clicking this button--> <button type="button" name="addFile" id="addFile" class="btn btn-success">Add Another Form</button>
</td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
这是AJAX代码,可帮助程序运行而无需刷新和添加像循环这样的表单
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td> <input type="text" name="name[]" class="form-control-file" id="exampleFormControlFile1"> <textarea class="form-control" name="areaName[]" rows="5" id="comment"></textarea><input type="file" name="fileName[]" class="form-control-file" id="exampleFormControlFile1"><button type="button" name="addFile" id="addFile" class="btn btn-success">Add Another Form</button></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$('#submit').click(function(){
$.ajax({
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
<script>
$(document).ready(function(){
var i=1;
$('#addFile').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="file" name="fileName[]" class="form-control-file" id="exampleFormControlFile1"></button></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
});
</script>
答案 0 :(得分:0)
您应该使用event delegation触发事件。见下文-
$(document).ready(function() {
$(document).on('click', '.add', function() {
var len = $('.form-row').length;
$('#dynamic_field').append('<tr class="form-row" data-id="'+len+'"><td> <input type="text" name="name[]" class="form-control-file" id="exampleFormControlFile1"> <textarea class="form-control" name="areaName[]" rows="5" id="comment"></textarea><input type="file" name="fileName[]" class="form-control-file" id="exampleFormControlFile1"><button type="button" name="addFile" id="addFile" class="btn btn-success addFile">Add Another Form</button></td><td><button type="button" name="remove" class="btn btn-danger btn_remove_all">X</button></td></tr>');
});
$(document).on('click', '.submit', function() {
$.ajax({
method: "POST",
data: $('#add_name').serialize(),
success: function(data) {
alert(data);
$('#add_name')[0].reset();
}
});
});
$(document).on('click', '.addFile', function() {
var id = $(this).closest('.form-row').data('id');
var elem = '<tr class="'+id+'"><td><input type="file" name="fileName[]" class="form-control-file" id="exampleFormControlFile1"></button></td><td><button type="button" name="remove" class="btn btn-danger btn_remove">X</button></td></tr>';
$(elem).insertAfter($(this).closest('tr'));
});
$(document).on('click', '.btn_remove, .btn_remove_all', function() {
if($(this).hasClass('btn_remove_all')){
var id = $(this).closest('.form-row').data('id');
$('tr.'+id).remove();
}
$(this).closest('tr').remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr class="form-row">
<td><input type="text" name="name[]" class="form-control-file" id="exampleFormControlFile1">
<textarea class="form-control" name="areaName[]" rows="5" id="comment"></textarea>
<input type="file" name="fileName[]" class="form-control-file" id="exampleFormControlFile1"><br>
<!--I Want to add another File Button after clicking this button--><button type="button" name="addFile" id="addFile" class="btn btn-success addFile">Add Another Form</button>
</td>
<td><button type="button" name="add" id="add" class="btn btn-success add">Add More</button></td>
</tr>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info submit" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
此外,请勿将id
用于元素上的触发事件,因为它的唯一性,因此,如果元素将重复,则事件不会在其他元素上触发。