在本人的網站交友樂上需要點擊一個重置按鈕,將所有的checkbox設置爲未選中,採用遍歷的方法來完成
控件如下
<button id="ClearPosition" type="button">清空職位</button>
<input type="checkbox" class="jobPostaJoblist"/><span>導購/促銷</span>
<input type="checkbox" class="jobPostaJoblist"/><span>收銀員</span>
<input type="checkbox" class="jobPostaJoblist"/><span>店長</span>
JQUERY代碼如下:
jobPostaJoblistSelected是checkbox被選中時的css
stopDefault函數用於中止默認的事件響應
$("#ClearPosition").each(function(i) {
$(this).click(function(e) {
$("input[class*='jobPostaJoblist']").each(function(i) {
$(this).attr("checked", false);
$(this).next().removeClass("jobPostaJoblistSelected");
});
return stopDefault(e);
});
});
function stopDefault(e) {
if (e && e.preventDefault) {
e.preventDefault();
} else {
window.event.returnValue = false;
}
return false;
}