[英]Prevent parent category radio button from being clicked
我有此功能/脚本,可将复选框转换为单选按钮(在Categories metabox中),但是我需要稍微扩展一下功能,但不确定如何去做。
剧本:
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的子项。
因此,规则应该是:如果您是父母,则不能选择您,但您的孩子可以。
取决于输出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);
});
甚至更好的是,删除广播:
jQuery("#categorychecklist > li > ul").each(function(){
jQuery(this).prev('label').children('input').remove();
});
请检查脚本代码中的注释。
$(function() { $('input[type=radio]').on('click', function() { //assumes that radio button > wrapped around a label > wrapped around li var liObj = $(this).parent().parent(); if (liObj != undefined && $(liObj).is('li') && $(liObj).has('ul').length > 0) { return false; } }); })
ul { list-style: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id='test'> <li> <label> <input type='radio' name='cat' />1</label> </li> <li> <label> <input type='radio' name='cat' />2</label> </li> <li> <label> <input type='radio' name='cat' />3</label> <ul> <li> <label> <input type='radio' name='cat' />3-1</label> </li> <li> <label> <input type='radio' name='cat' />3-2</label> <ul id='test'> <li> <label> <input type='radio' name='cat' />3-2-1</label> </li> <li> <label> <input type='radio' name='cat' />3-2-2</label> </li> </ul> </li> </ul> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.