繁体   English   中英

如果选中了wordpress metabox复选框,如何执行jquery功能同步添加新帖子区域

[英]how to do jquery function if wordpress metabox checkbox is checked Sync in add new post area

在wordpress后端->添加新帖子区域中,如何做某事选中metabox->复选框后立即进行操作,例如,当我检查类别时:

这是选中前的复选框:

<input value="1" type="checkbox" name="post_category[]" id="in-category-1">

这是选中后的复选框:

<input value="1" type="checkbox" name="post_category[]" id="in-category-1">
::before
</input>

那么,如何检测复选框已选中,并且选中后立即起作用?

您可以使用jQuery来实现。

!(function($){
$(document).ready(function(){
    $('#categorychecklist').on('change', 'input[type=checkbox]', function(){
        if ($(this).is(':checked'))
            alert('Category' + $(this).closest('label').text() + ' added.');
        else
            alert('Category' + $(this).closest('label').text() + ' removed.');
    });
});
})(window.jQuery);

/wp-content/themes/themename/js/post-jquery.js

我要说的是在选中或取消选中它们之后添加或删除了哪个类别(更改事件)。

附加此jQuery。 我像这样检查当前页面是否正在使用get_current_screen()添加/编辑帖子。

<?php
function add_script_to_post() {
    $current_screen = get_current_screen();
    if ( $current_screen->base == 'post' ) { // Check if is adding/editing post
        wp_enqueue_script('post-jquery', get_stylesheet_directory_uri() . '/js/post-jquery.js');
    }
}
add_action('current_screen', 'add_script_to_post');
?>

/wp-content/themes/themename/functions.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM