2016-07-25 50 views
1

Got this code in the following question 嗨! 如何將此代碼轉換爲「通用」。目前它只適用於一種情況。如果我有兩個它不會工作。隱藏不同檢查比例的不同divs

謝謝

$(document).ready(function() { 
 
    $('input[type="radio"]').click(function() { 
 
     if($(this).attr('id') == 'watch-me') { 
 
      $('#show-me').show();   
 
     } 
 
     else { 
 
      $('#show-me').hide(); 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="medium-12"> 
 
    <label>Are you a member?</label> 
 
</div> 
 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div id="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div>

+1

使用類名來代替IDS – DaniP

+0

通用:相對緩慢的過濾器表達式(:radio)僅在活動時間運行時(沒關係)? –

+0

什麼不起作用? – demo

回答

2

你必須設置一個共同的class,做DOM遍歷相對於所選擇的單選按鈕。


事情要注意的是:

  1. 使用更改事件,而不是爲單選按鈕,單擊事件。
  2. 使用該值來檢測單選按鈕,而不是嗅探其ID。

$(document).ready(function() { 
 
    $('input[type="radio"]').change(function() { 
 
     $(this).closest('fieldset').find('.show-me').toggle(this.value == "yes");   
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="medium-12"> 
 
    <label>Are you a member?</label> 
 
</div> 
 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div> 
 

 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div> 
 

 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div>

+1

@RaRajaprabhu Aravindasamy令人驚歎,完美! 謝謝! – grcoder

+0

注意:如果使用附加到'document'的委託事件處理程序,則不需要DOM就緒處理程序,並且連接到大量元素的效率更高。 –

+0

@GoneCoding代替文檔,我們必須使用最近的靜態父級來附加委託事件處理程序以獲得更多性能。 –

2

如果我有兩個將無法正常工作。

爲什麼? ::因爲如果你只是複製/粘貼相同的結構兩次,你將有重複的ID,這是無效的,使你的JS代碼無用。

你需要的是基於類名或ATTR像這樣的結構:

$(document).ready(function() { 
 
    $('input[type="radio"]').click(function() { 
 
     if($(this).attr('value') == 'yes') { 
 
      $(this).siblings('.show-me').show();   
 
     } 
 
     else { 
 
      $(this).siblings('.show-me').hide(); 
 
     } 
 
    }); 
 
});
.show-me { 
 
    display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="medium-12"> 
 
    <label>Are you a member?</label> 
 
</div> 
 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div> 
 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me2" value="yes" name="Member2" class="tap-input"> 
 
    <label for="watch-me2">Yes</label> 
 
    <input type="radio" id="MemberNo2" value="no" name="Member2" class="tap-input"> 
 
    <label for="MemberNo2">No</label> 
 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber2">Please enter your Membership number</label> 
 
     <input id="memberNumber2" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div>

+0

謝謝!我正在嘗試這樣。我應該去上課和價值attr !.很棒 – grcoder

1

因爲你可以做這樣的事情不同的值。

function showHide(input){ 
 
    var attrVal = $(input).attr('id'); 
 
    switch (attrVal) { 
 
     case 'watch-me': 
 
     $('#show-me-2').hide(); 
 
     $('#show-me').show();  
 
     break; 
 
     case "watch-me-maybe": 
 
     $('#show-me').hide(); 
 
      $('#show-me-2').show(); 
 
      break; 
 
     default : 
 
     $('#show-me-2').hide(); 
 
     $('#show-me').hide(); 
 
      break; 
 
     } 
 
} 
 
$(document).ready(function() { 
 
    $('input[type="radio"]').each(function(){ 
 
     showHide(this); 
 
    }); 
 
    $('input[type="radio"]').click(function() { 
 
    showHide(this); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="medium-12"> 
 
    <label>Are you a member?</label> 
 
</div> 
 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input" checked="checked"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 
    <input type="radio" id="watch-me-maybe" value="maybe" name="Member" class="tap-input"> 
 
    <label for="MemberMaybe">Maybe</label> 
 

 
    <div id="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    <div id="show-me-2" class="medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber2">Please enter your Membership number2</label> 
 
     <input id="memberNumber2" name="memberNumber2" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div>

+0

非常感謝。由於其簡單性,我選擇了RajaprabhuAravindasamy答案。再次謝謝你! – grcoder

1

我試圖使這項工作沒有考慮考慮任何classid,只是html結構。

看到這裏jsfiddle

  1. 從未使用重複的ID!不要把相同的ID在2個或更多不同的元素,使用類,而不是

  2. 使用attr('value')找到自己的元素,而不是attr('id')

JQ:

$("fieldset").each(function(){ 
var showme = $(this).children("div"), 
    radio = $(this).children('input[type="radio"]') 
$(radio).change(function() { 
    if($(this).attr('value') == 'yes') { 
     $(showme).show();   
    } 
    else { 
     $(showme).hide(); 
    } 
}); 
}); 

讓我知道,如果它幫助

+0

親愛的@MihaiT,非常感謝,但我選擇了RajaprabhuAravindasamy答案,因爲它的簡單性。再次謝謝你! – grcoder

+0

我看到了答案。這是一個非常好的。在我的回答中,你可以看到,我沒有使用任何'class'或'id'。試圖儘可能使其成爲'通用':) –

1

更短的版本給你。它使用附加到文檔的委託事件(它始終存在,因此不需要DOM就緒處理程序)。在這個意義上

$(document).on('change', ':radio', function() { 
 
    $(this).closest('fieldset').find('.show-me').toggle(this.value == "yes");   
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="medium-12"> 
 
    <label>Are you a member?</label> 
 
</div> 
 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div> 
 

 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div> 
 

 
<div class="medium-6"> 
 
    <fieldset class="form-row" id="Member"> 
 
    <input type="radio" id="watch-me" value="yes" name="Member" class="tap-input"> 
 
    <label for="watch-me">Yes</label> 
 
    <input type="radio" id="MemberNo" value="no" name="Member" class="tap-input"> 
 
    <label for="MemberNo">No</label> 
 

 
    <div class="show-me" class=" medium-12"> 
 
     <div class="form-row"> 
 
     <label for="memberNumber">Please enter your Membership number</label> 
 
     <input id="memberNumber" name="memberNumber" class="inputfield" type="text"> 
 
     </div> 
 
    </div> 
 
    </fieldset> 
 
</div>

+0

謝謝你。我已經測試過了。我會用它:) – grcoder