Javascript 如果通过javascript选中另一个复选框,如何自动取消选中一个复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2947569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:37:59  来源:igfitidea点击:

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

javascript

提问by Sandipan

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.

我有一个复选框出生日期,它只显示 mm/dd。在它下面有另一个名为 ShowYear 的复选框,它显示年份,并且这个复选框只有在选中了出生日期复选框时才可见。

Now I want to uncheck the ShowYear checkbox automatically if the Birthdate checkbox is unchecked through javascript.

现在,如果通过 javascript 取消选中出生日期复选框,我想自动取消选中 ShowYear 复选框。

回答by bla

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

回答by Sarfraz

First, give your check boxes idseg:

首先,给你的复选框,ids例如:

<input type="checkbox" id="birth" />
<input type="checkbox" id="year" />

Javascript:

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>

回答by Pranay Rana

If you are using jquery than

如果你使用 jquery 比

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