简体   繁体   English

在使用jQuery的最后一个元素之后插入不起作用

[英]insert after last element using jQuery is not working

HTML: 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: 这是我简单的jquery代码,用于添加表单的一行:

 $(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 : Ajax动作:

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(); 还有show_slider_form_input(); definition is as follows which is inside absb_bt_to_s-admin-display.php: 定义如下,位于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); 似乎$('。btts-form-group:last')。after(response); is executed two times. 被执行两次。 I also added e.stopImmediatePropagation(); 我还添加了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 您的代码运行正常,没有任何问题,并确保您的ajax响应,就您而言,您的ajax响应就是问题

 $(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 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM