HTML:
<form enctype="multipart/form-data" action="" method="POST" style="margin-top: 20px; ">
<div class="row btts-form-group clearfix">
<div style="width: 20%; float: left; margin-right: 1%;">
Image : <input type="file" name="image[]">
</div>
<div style="width: 27%; float: left; margin-right: 1%;">
Title : <input type="text" name="title[]">
</div>
<div style="width: 22%; float: left; margin-right: 1%;">
More Info : <input type="text" name="more_info[]">
</div>
<div style="width: 22%; float: left; margin-right: 1%;">
Button : <input placeholder="Put the url.." type="text" name="button_link[]">
</div>
<div style="width: 5%; float: left; ">
<input type="button" class="button button-primary add_more" value="+">
</div>
</div>
<input type="submit" class="button button-primary" value="Save Change" style="margin-top: 20px;">
</form>
Here is my simple jquery code to append a row of a form:
$(document).ready(function(){
$('body').on('click', '.add_more', function(e){
$.ajax({
url : absbBtToS.ajax_url,
type : 'get',
data : {
action : 'new_slider_html',
security : absbBtToS.check_nonce
},
success : function( response ) {
$('.btts-form-group:last').after(response);
//console.log(response);
//jQuery('.rml_contents').html(response);
},
error : function(error){
console.log(error);
}
});
e.stopImmediatePropagation();
});
});
Ajax Action :
add_action( 'wp_ajax_new_slider_html', 'new_slider_html');
function new_slider_html(){
include( plugin_dir_path( __FILE__ ) . 'admin/partials/absb_bt_to_s-admin-display.php');
if( !check_ajax_referer( 'absbBtToS-nonce', 'security' ) ){
wp_send_json_error('error!');
}
show_slider_form_input();
}
And show_slider_form_input();
definition is as follows which is inside absb_bt_to_s-admin-display.php:
function show_slider_form_input(){?>
<div class="row btts-form-group clearfix" >
<div style="width: 20%; float: left; margin-right: 1%;">
Image : <input type="file" name="image[]" />
</div>
<div style="width: 27%; float: left; margin-right: 1%;">
Title : <input type="text" name="title[]" />
</div>
<div style="width: 22%; float: left; margin-right: 1%;">
More Info : <input type="text" name="more_info[]" />
</div>
<div style="width: 22%; float: left; margin-right: 1%;">
Button : <input placeholder="Put the url.." type="text" name="button_link[]" />
</div>
<div style="width: 5%; float: left; ">
<input type="button" class="button button-primary add_more" value="+" />
</div>
</div>
<?php }
But when I click once. Two row added. it seems $('.btts-form-group:last').after(response); is executed two times. I also added e.stopImmediatePropagation();
but no luck. Any idea?
your code is working fine without any problem and make sure that your ajax response, In your case your ajax response is the problem
$(document).ready(function(){ $('body').on('click', '.add_more', function(e){ var arr=['<div class="row btts-form-group clearfix" >','<div style="width: 20%; float: left; margin-right: 1%;">','Image : <input type="file" name="image[]" />','</div>','<div style="width: 27%; float: left; margin-right: 1%;">','Title : <input type="text" name="title[]" />','</div>','<div style="width: 22%; float: left; margin-right: 1%;">','More Info : <input type="text" name="more_info[]" />','</div>','<div style="width: 22%; float: left; margin-right: 1%;">','Button : <input placeholder="Put the url.." type="text" name="button_link[]" />','</div>','<div style="width: 5%; float: left; ">','<input type="button" class="button button-primary add_more" value="+" />','</div>','</div>']; var response=arr.join(""); $('.btts-form-group:last').after(response); /*$.ajax({ url : absbBtToS.ajax_url, type : 'get', data : { action : 'new_slider_html', security : absbBtToS.check_nonce }, success : function( response ) { $('.btts-form-group:last').after(response); //console.log(response); //jQuery('.rml_contents').html(response); }, error : function(error){ console.log(error); } });*/ e.stopImmediatePropagation(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form enctype="multipart/form-data" action="" method="POST" style="margin-top: 20px; "> <div class="row btts-form-group clearfix"> <div style="width: 20%; float: left; margin-right: 1%;"> Image : <input type="file" name="image[]"> </div> <div style="width: 27%; float: left; margin-right: 1%;"> Title : <input type="text" name="title[]"> </div> <div style="width: 22%; float: left; margin-right: 1%;"> More Info : <input type="text" name="more_info[]"> </div> <div style="width: 22%; float: left; margin-right: 1%;"> Button : <input placeholder="Put the url.." type="text" name="button_link[]"> </div> <div style="width: 5%; float: left; "> <input type="button" class="button button-primary add_more" value="+"> </div> </div> <input type="submit" class="button button-primary" value="Save Change" style="margin-top: 20px;"> </form>
I hope it will helpful to you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.