我为我的自定义分类法编写了一些额外的字段。我将表单字段添加到添加表单中。我通过在中处理适当的值来保存它$_POST
. 在4.1中。x、 添加表单是Ajax化的。如何清除成功的表单字段?
更具体地说,我如何查看对add-tag
管理ajax操作?
我为我的自定义分类法编写了一些额外的字段。我将表单字段添加到添加表单中。我通过在中处理适当的值来保存它$_POST
. 在4.1中。x、 添加表单是Ajax化的。如何清除成功的表单字段?
更具体地说,我如何查看对add-tag
管理ajax操作?
您可以通过一点jQuery来实现这一点:
在文件夹/js/called字段clear中创建此文件。js和替换#formID
使用要重置的表单ID:
//Code runs when an ajax request succeeds
jQuery( document ).ajaxSuccess(function( event, xhr, settings ) {
//Check ajax action of request that succeeded
if(settings.action == \'add_tag\') {
//Reset the form
jQuery(\'#formID\')[0].reset();
//Send ajax response to console.
console.log("Triggered ajaxSuccess handler. The ajax response was: " + xhr.responseText );
}
});
然后在函数中。php加载javascript:wp_register_script(\'field-clear-script\', get_stylesheet_directory_uri() . \'/js/field-clear.js\', array(\'jquery\'), null, true);
function field_clear_load_scripts() {
wp_enqueue_script(\'field-clear-script\');
}
add_action(\'admin_enqueue_scripts\', \'field_clear_load_scripts\');
编辑-添加代码以显示ajax响应文本。添加了对ajax操作参数的检查。将enqueue函数更改为在admin上排队,因为我认为这是必需的。Jason\'s answer 非常接近,这为我在WP 4.9上提供了此解决方案:
$(document).ajaxSuccess(function(evt, xhr, opts) {
if (
xhr.status >= 200
&& xhr.status < 300
&& opts.data
&& /(^|&)action=add-tag($|&)/.test(opts.data)
&& /(^|&)taxonomy=YOUR_TAXONOMY($|&)/.test(opts.data)
&& xhr.responseXML
&& $(\'wp_error\', xhr.responseXML).length == 0
) {
// clear your fields
}
});
我正在从事这个项目,在保存帖子之前,我正在尝试执行jquery或javascript,我尝试了以下方法: function do_something_with_a_post($id) { <script>alert(\'hello\');</script> } add_action(\'pre_post_update\', \'do_something_with_a_post\'); 但它给出了一个php错误。。。我真的不知道该怎么