php - Ajax 表单只能工作一次

标签 php javascript ajax wordpress

我的脚本看起来像这样。 是的,它来自插件。 ,我对 ajax 不太满意。

如何让此表单刷新并能够在首次提交后再次接受输入?

<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>

非常感谢。

编辑:

回调函数以die()结束。我读到这只是为了能够检索函数的输出。这是否也结束了 AJAX?

最佳答案

要启用此功能,您可以将 disabled 属性设置为 false

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

我还会更改您的代码以禁用它;

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

来自jQuery docs :

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

关于php - Ajax 表单只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191355/

相关文章:

php - 在 footer.php Wordpress 中添加脚本 jquery

javascript - 使用 ajax 和 php 向数据库添加值

javascript - ajax的授权和cors的二次响应如何显示内容?

javascript - 通过列渲染将jquery数据表的行ID传递给另一个函数

php - 将 PHP 日期字符串作为时间戳保存到 MySQL 数据库中

javascript - 我如何制作产品定制生成器

php - 如何在子文件夹中的 laravel 中获取配置数据

javascript - 如何使表单动态自动完成

javascript - 在 CoffeeScript 中解析参数和匿名函数

Javascript 复选框检查