[英]How to automatically uncheck a checkbox if another checkbox is checked through javascript?
我有一個復選框Birthdate,僅顯示mm / dd。在它下面還有另一個名為ShowYear的復選框,它顯示年份,只有選中Bithdate復選框時才會顯示此復選框。
現在,如果通過javascript取消選中Birthdate復選框,我想自動取消選中ShowYear復選框。
<input id="cbxBirthDate" type="checkbox" onClick="javascript:uncheckShowYear(this);" />
<input id="cbxShowYear" type="checkbox" />
<script language="javascript" type="text/javascript">
function uncheckShowYear(obj)
{
if (obj.checked == false)
{
document.getElementById("cbxShowYear").checked = false;
}
}
</script>
首先,給你的復選框ids
例如:
<input type="checkbox" id="birth" />
<input type="checkbox" id="year" />
使用Javascript:
<script type="text/javascript">
window.onload = function(){
var birth = document.getElementById('birth');
var year = document.getElementById('year');
if (birth.checked == false)
{
year.checked = false;
}
}
</script>
如果你使用的是jquery而不是
$(document).ready( function a()
{
if ( $('#birth').is(':checked'))
{
$('#year').attr('checked', false);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.