我有一個簡單的函數,當用戶點擊給定的複選框時顯示一個div。我想在另一個複選框上具有相同的行爲,所以我想將它概括爲傳遞要顯示的元素的函數。定義泛型函數jquery
但我不知道JQuery的語法來這樣做。它在頁面加載時自動觸發。有人有想法嗎?
代碼:
$(document).ready(function() {
$("#transcricao").change(
function(){
if ($('.form_transcr').css('display') === 'none') {
$('.form_transcr').fadeIn();
} else {
$('.form_transcr').fadeOut();
}
}
); //This is working fine!
$("#traducao").change(show_hide($('.form_trad')));
//This is auto-trigerring without user action...
});
這裏是我的功能:
function show_hide($elm){
//This is the "generalized" function that I'd like to use on both
//checkboxes, just passing the element.
if ($($elm).css('display') === 'none') {
$($elm).fadeIn();
} else {
$($elm).fadeOut();
}
}
這不就是'fadeToggle()'呢? – Barmar