Ajax form only work one time

2019-08-04 00:46发布

My script is looking like this. Yes, it is from a plugin. No, I'm not comfortable with ajax.

How can I get this form to refresh and be able to take input once more after first submit?

<script>
jQuery(document).ready(function() { 
    var options = { 
        target: '#ps_output',      // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,     // pre-submit callback 
        success:       showResponse,    // post-submit callback 
        url:           '<?php echo $link; ?>'         // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php     
}; 

    // bind form using 'ajaxForm' 
    jQuery('#thumbnail_upload').ajaxForm(options); 
});

function showRequest(formData, jqForm, options) {
    //do extra stuff before submit like disable the submit button
    jQuery('#ps_output').html('Sending...');
    jQuery('#submit-ajax').attr("disabled", "disabled");
}

function showResponse(responseText, statusText, xhr, $form)  {
    //do extra stuff after submit
    jQuery('#submit-ajax').attr("enabled", "enabled");
}
</script>

Thanks alot.

EDIT:

The callback function ends in die(). I read that was simply for being able to retrieve the output from the function. Does that end AJAX too?

2条回答
家丑人穷心不美
2楼-- · 2019-08-04 00:58

Use

jQuery('#submit-ajax').removeAttr("disabled");//to remove disabled attribue

instead of

jQuery('#submit-ajax').attr("enabled", "enabled");
查看更多
ら.Afraid
3楼-- · 2019-08-04 01:20

To enable, you can set the disabled property to false.

jQuery('#submit-ajax').prop('disabled', false);

I would also change your code that disables it to;

jQuery('#submit-ajax').prop('disabled', true);

From the jQuery docs:

The .prop() method should be used to set disabled and checked instead of the .attr() method.

查看更多
登录 后发表回答