2017-01-15 7 views
0

チェックボックスをラジオボタン(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の子供の項目は選択することができます。

したがって、ルールは次のようにする必要があります。親であれば選択できませんが、子供はできます。

Category radio buttons

+0

この部分だけレンダリングされたhtmlを共有できますか?トラバーサルを見るのに役立つでしょう。 – Searching

答えて

1

あなたの出力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(); 
}); 
+0

ブラボー - それは完璧に動作します。ラジオを見えるようにして、ユーザーが(限定された)階層を見ることができるようにしました。 – user3256143

0

スクリプトコード内のコメントをご確認ください。

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

関連する問題