對於input的radio,checkbox類型而言,他是沒有文本屬性的
<input type="radio" name="person" value="1" onclick="choseperson(this)"><label style="font-size:14px;">男</label>
這里如果通過 function choseperson(target){ var val=$(target).val();//拿到value值即是1,想要拿到文本:男 就要使用 $(target).next("label").text(); }
有時候為了達到checkbox的互斥效果。我們需要根據點擊當前的元素,來達到目的。
在同一個div里有多個checkbox,當我們選擇其中一個checkbox時,我們要傳入this
<input type="checkbox" name="hobby" value="1" onclick="chose(this)"><label style="font-size:14px;">音樂</label>
那么在function chose(tar){
//先拿到他的父元素,再在父元素里尋找。
$(tar).parent().find('input[type="checkbox"]').each(function() {
if ($(this).is(':checked')) {
// 得到已選擇的checkbox框。
}
}
});
}