最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 automatically if the Birthdate checkbox is unchecked through javascript.

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 automatically if the Birthdate checkbox is unchecked through javascript.

Share Improve this question asked Jun 1, 2010 at 6:03 SandipanSandipan 2192 gold badges5 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9
<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)

  1. 暂无评论