我有这个函数/脚本,可以将复选框转换为单选按钮(在类别元框中),但我需要稍微扩展一下功能,但我不确定如何进行。
脚本:
function convert_root_cats_to_radio() {
global $post_type;
?>
<script type="text/javascript">
jQuery("#categorychecklist>li>input").each(function(){
this.disabled = "disabled";
});
jQuery("#categorychecklist>li>ul>li>input").each(function(){
this.disabled = "disabled";
});
jQuery("#categorychecklist>li>label input").each(function(){
this.type = 'radio';
});
jQuery("#categorychecklist>li>ul>li>label input").each(function(){
this.type = 'radio';
});
// Hide the 'most used' tab
jQuery("#category-tabs li:odd").hide();
</script> <?php
}
add_action( 'admin_footer-post.php', 'convert_root_cats_to_radio' );
add_action( 'admin_footer-post-new.php', 'convert_root_cats_to_radio' );
现在需要做什么:防止用户选择父类别。
例如,在下图中,您应该能够选择除 Bandicoot 之外的任何内容,因为 Bandicoot 是父级(它有子级)。哦,Bandicoot 的 child 项目是可以选择的。
所以规则应该是:如果您是 parent ,您就不能被选中,但您的 child 可以。
最佳答案
根据您的输出 html 的外观,您可以使用以下选项之一:
jQuery("#categorychecklist > li > ul").each(function(){
jQuery(this).parent('li').children('label').children('input').attr('disabled', true);
});
或者:
jQuery("#categorychecklist > li > ul").each(function(){
jQuery(this).prev('label').children('input').attr('disabled', true);
});
或者更好的是,删除 radio :
jQuery("#categorychecklist > li > ul").each(function(){
jQuery(this).prev('label').children('input').remove();
});
关于javascript - 防止单击父类别单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41667321/