2012-10-27 54 views

回答

0

以下是我的網站中的一些代碼,用於創建自定義帖子類型元框。它使用jQuery驗證類別值,如果該值不存在,則取消表單提交。

add_action('load-post.php', 'gallery_meta_boxes_setup'); 
add_action('load-post-new.php', 'gallery_meta_boxes_setup'); 

/* Meta box setup function. */ 
function gallery_meta_boxes_setup() { 
    /* Add meta boxes on the 'add_meta_boxes' hook. */ 
    add_action('add_meta_boxes', 'gallery_add_post_meta_boxes'); 

} 
/* Create one or more meta boxes to be displayed on the post editor screen. */ 
function gallery_add_post_meta_boxes() { 

    add_meta_box(
     'gallery-class',   // Unique ID 
     'Select Gallery',  // Title 
     'gallery_class_meta_box',  // Callback function 
     'cj-gallery',     // Admin page (or post type) 
     'normal',     // Context 
     'default'     // Priority 
    ); 
} 

/* Display the post meta box. */ 
function gallery_class_meta_box($object, $box) { 
?> 
    <script type="text/javascript"> 
     jQuery(function($) { 
       /********** Form Validation ***********/ 
      $('form').submit(function(event) { 

       if ($('.categorydiv input[type="checkbox"]:checked').length == 0) { 
        alert('Please select a category!'); 
        $('#ajax-loading').css('visibility', 'hidden'); // hide the ajax loading graphic 
        event.preventDefault(); // cancel form submission 
       } 
      }) 
     }); 
    </script> 
<?php } 
+0

嘿,謝謝你的回答。我只是在'$('#ajax-loading')。css('visibility','hidden');''後面添加'$('input#publish')。removeClass('button-primary-disabled');'' – m0g