javascript - 防止单击父类别单选按钮

标签 javascript jquery wordpress function categories

我有这个函数/脚本,可以将复选框转换为单选按钮(在类别元框中),但我需要稍微扩展一下功能,但我不确定如何进行。

脚本:

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 可以。

Category radio buttons

最佳答案

根据您的输出 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/

相关文章:

javascript - Birt:通过脚本改变图表尺寸

Javascript 动态事件分配表现得很奇怪

javascript - 使用 jQuery 删除表但不删除表单和字段

jQuery - 如何将处理程序附加到尚不存在的元素?

JavaScript/CSS 悬停功能

validation - 自定义验证在 4.1.1 版的联系表 7 中不起作用

javascript - Canvas2Image 或 HTML5 下载属性的跨浏览器替代品?

javascript - 此 JavaScript 文件在一个页面上运行,但在其他页面上不运行,这是为什么?

javascript - 自动从 Woocommerce 谢谢重定向到传递变量的外部链接

php - 从 WooCommerce 中的订单项目中获取变体的自定义值