1

こんにちは、私は助けを得ることを望んでいるいくつかのコードに行き詰まっています。私は次のようにhtmlを設定しています:


     <div class="menu_options">
           <div class="parent_options">
               <input type="checkbox" class="parent_required" />
           </div>
           <div class="children">
                 <div class="child_options">
                      <input type="checkbox" class="child_required" />   
                 </div>
                 <div class="child_options">
                      <input type="checkbox" class="child_required" />   
                 </div>
                 <div class="child_options">
                      <input type="checkbox" class="child_required" />   
                 </div>
           </div>
      </div>

基本的に私がやろうとしているのはjqueryで、parent_requiredクラスのチェックボックスがチェックされている場合、子クラスの下のすべての入力フィールドを取得し、child_requiredによってすべての入力フィールドクラスをチェック解除します。

使用するフィールドのすべての ID がわからないため、入力フィールドで id' を使用できません。

これまでjqueryで私はこのコードを持っています

      $('.parent_required').click(function() {

            if(!$(this).is(":checked")) {
                var child = $(this).parent('.parent_options').siblings('.children');

            }
        });

そして、私はここで div class='children の部分を吸う方法を知りません。

4

5 に答える 5

3

これを試して:

$('.parent_required').click(function() {
    if (!$(this).is(":checked")) {
        $(this).closest('.parent_options')
            .siblings('.children')
            .find('.child_required')
            .prop('checked', false);
    }
});

フィドルの例

于 2012-12-17T17:17:57.990 に答える
1
$('.parent_required').click(function() {

  if(!$(this).is(":checked")) {
    // this gives you the <div class="children">
    var child = $(this).closest('. menu_options').find('.children');
  }
});
于 2012-12-17T17:19:23.357 に答える
1

私はparent_requiredchangeの代わりに使用します:click

$('.parent_required').change(function() {

    if (!$(this).is(":checked")) {
        $('.parent_required').parents('.menu_options').find('.child_required').prop('checked', false);
    }
});
于 2012-12-17T17:20:12.633 に答える
1

ID がわからない場合は、入力のノード名、タイプ属性、またはクラスを使用してください。絶対 DOM パスを使用する理由はありません。

また、parent呼び出しは のみを返しますが、そこから子を選択<div class="parent_options">するには が必要です。<div class="menu_options">

$('.parent_required').click(function() {
    if (!this.checked)
        $(this)
          .closest('.menu_options')
          .find(':checkbox.child_required')
          .prop('checked', false);
});
于 2012-12-17T17:23:03.350 に答える
0

これをチェックして:

    $('.parent_required').on('change', function() {

            if(!$(this).is(":checked")) {
               $(this).closest('.parent_options')
                      .next('.children').find('.child_required').attr('checked', false);
            }
        });

JSBin: http://jsbin.com/evoreq/1/edit

ここでは、closest「parent_options」が見つかるまで親チェックボックスからメソッドを上に移動し、「children」クラスを持つ「次の」要素を取得し、その中のすべての「child_required」要素を見つけて、checked 属性をに設定します。間違い。

于 2012-12-17T17:22:48.247 に答える