开发者

How to automatically uncheck a checkbox if another checkbox is checked through javascript?

开发者 https://www.devze.com 2023-01-01 23:42 出处:网络
I have a checkbox Birthdate which shows the mm/dd only.And below it there is another checkbox called ShowYear which shows the year and this checkbox is only visible if the Bithdate checkbox is checked

I have a checkbox Birthdate which shows the mm/dd only.And below it there is another checkbox called ShowYear which shows the year and this checkbox is only visible if the Bithdate checkbox is checked.

Now I want to uncheck the ShowYear checkbox au开发者_运维知识库tomatically if the Birthdate checkbox is unchecked through javascript.


<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>


First, give your check boxes ids eg:

<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>


If you are using jquery than

$(document).ready( function a()
{
   if (  $('#birth').is(':checked'))
    {
       $('#year').attr('checked', false);
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号