2011-07-15 99 views

回答

4

您可以使用wp-admin/js/common.js中的基本客户端表单验证脚本,该脚本默认包含在所有管理页面中。

首先,添加通过插件的后期编辑屏幕上运行的动作:

add_action('admin_print_scripts', 'my_validation_script'); 
function my_validation_script() { 
    global $post_type; 
    if(isset($post_type) && $post_type == 'knowledgebase_article') { 
     wp_enqueue_script('my-validation-script', network_site_url() . '/wp-content/plugins/your-plugin-folder/my-validation-script.js', array('jquery', 'common')); 
    } 
} 

接下来,写一些JavaScript形式处理实际处理表单提交:

jQuery(document).ready(function(){ 

    //'my_required_field' is the class name of the DIV or TD tag *containing* the required form field 
    jQuery('.my_required_field').addClass('form-required'); 

    //'post' is the ID of the main form on the new post screen 
    jQuery('#post').submit(function(){ 
      //the validateForm function lives in wp-admin/js/common.js 
      if (!validateForm(jQuery(this))) { 
        alert('Please enter a value'); 
        return false; //stop submitting the form 
      } else { 
        //alert('all required fields have some input'); 
      } 
    }); 

}); 

你可以通过访问标签编辑页面来查看验证器的行为,然后创建一个没有名字的新标签。该字段的容器变成红色,提醒您遇到问题。

我在我的测试结束,它的工作。希望它能帮助你。

+0

感谢您的意见,我会在星期一上班时给它一个镜头。 :-) – cynicaloptimist

+0

这个解决方案是现货。我不知道为什么我的问题因“模糊”而关闭,你完全明白我在问什么。 – cynicaloptimist

+0

我想知道“模糊”的同一件事。感谢+1 – hardy101

相关问题