提交后未加载脚本

时间:2017-07-18 10:10:50

标签: php jquery wordpress

我在页面末尾有这个脚本(projects.php)

 $( '[data-trigger-modal]' ).on( 'click', function(){
    alert("finaly");
    var campaign_id = $( this ).data( 'campaign-id' ),

        $wrapper = $( '#charitable-donation-form-modal-loop .donation-form-wrapper' );

    if ( ! campaign_id ) {
        return;
    }

    $wrapper.html( "<img src=\"<?php echo charitable()->get_path( 'assets', false ) ?>/images/charitable-loading.gif\" width=\"60\" height=\"60\" alt=\"<?php esc_attr_e( 'Loading&hellip;', 'charitable' ) ?>\" />" );

    resize_modal();

    $.ajax({
        type: "POST",
        data: {
            action : 'get_donation_form',
            campaign_id : campaign_id
        },
        dataType: "json",
        url: "http://www.afif.qa/wp-admin/admin-ajax.php",
        xhrFields: {
            withCredentials: true
        },
        success: function ( response ) {
            if ( response.success ) {

                $wrapper.html( response.data );




                resize_modal();                    

                return;
            }

            $wrapper.html( "<?php _e( 'Unfortunately, something went wrong while trying to retrieve the donation form. Please reload the page and try again.', 'charitable' ) ?>" );
        },
        error: function() {
            $wrapper.html( "<?php _e( 'Unfortunately, something went wrong while trying to retrieve the donation form. Please reload the page and try again.', 'charitable' ) ?>" );
        }
    }).fail(function ( response ) {
        if ( window.console && window.console.log ) {
            console.log( response );
        }
    });

    return false;
});

HTML

<a data-trigger-modal="charitable-donation-form-modal-loop" data-campaign-id="4057" class="donation-button" a>Donate now    </a>

这是一些页面中的提交按钮(projects.php)

<form name="searching" method="post">    
<div class="col-sm-2 col-md-2 col-lg-2 project_style">
          <button name="btn_get_result" type="submit"  class="btn btn-info col-xs-12" style="height: 40px;line-height: 0;border-radius:4px; margin-top: 27px;;"><?php _e( 'Search', 'AfifSearch' ); ?></button>
    </div>
</form>

现在问题:当我重新加载页面但在提交后无法正常工作时,脚本工作正常。

2 个答案:

答案 0 :(得分:0)

您需要使用name,因为您拥有该ID。以下是代码:

&#13;
&#13;
(function($) {
  $("button[name='btn_get_result']").click(function() {
    alert("Test script");
  });

})(jQuery);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-2 col-md-2 col-lg-2 project_style">
  <button name="btn_get_result" type="submit" class="btn btn-info col-xs-12" style="height: 40px;line-height: 0;border-radius:4px; margin-top: 27px;;"><?php _e( 'Search', 'AfifSearch' ); ?></button>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

(function($) {
   $(".donation-button").click(function() {
   alert("Success!");  
});
})(jQuery);