简体   繁体   English

防止单击父类别单选按钮

[英]Prevent parent category radio button from being clicked

I've got this function/script that converts the checkboxes into radio buttons (in the Categories metabox), but I need to extend the functionality a little but I'm unsure how to go about it. 我有此功能/脚本,可将复选框转换为单选按钮(在Categories metabox中),但是我需要稍微扩展一下功能,但不确定如何去做。

The script: 剧本:

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' );

What is needed now: to prevent users from selecting a parent category. 现在需要什么:防止用户选择父类别。

In the image shown below for example, you should be able to select anything except Bandicoot, because Bandicoot is a parent (it has children). 例如,在下面显示的图像中,您应该能够选择Bandicoot以外的任何内容,因为Bandicoot是父母(有孩子)。 Oh and the children items for Bandicoot are allowed to be selected. 哦,并且可以选择Bandicoot的子项。

So the rule should be: if you're a parent you can't be selected, but your children can. 因此,规则应该是:如果您是父母,则不能选择您,但您的孩子可以。

类别单选按钮

Depends on how your output html looks you can make it in one of below options: 取决于输出html的外观,您可以使用以下选项之一进行设置:

jQuery("#categorychecklist > li > ul").each(function(){
    jQuery(this).parent('li').children('label').children('input').attr('disabled', true);
});

or: 要么:

jQuery("#categorychecklist > li > ul").each(function(){
    jQuery(this).prev('label').children('input').attr('disabled', true);
});

or even better, remove radio: 甚至更好的是,删除广播:

jQuery("#categorychecklist > li > ul").each(function(){
    jQuery(this).prev('label').children('input').remove();
});

Please check the comment in the script code. 请检查脚本代码中的注释。

 $(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.

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