javascript 取消选中一个复选框,因为另一个被选中并提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16093177/
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-10-27 03:14:39  来源:igfitidea点击:

uncheck one checkbox as another is checked and submit the form

javascriptsubmituncheck

提问by user2170133

I am trying to combine two javascriptfunctions into one function.

我正在尝试将两个javascript功能合并为一个功能。

So, I have this chunk of code:

所以,我有这段代码:

<form name="form1" action="testing_page2.php" method="GET">
<p><input id="Main" type="checkbox" name="Main" value="1" onClick="javascript:uncheckSecondary(this);" />Main</p>
<p><input id="Secondary" type="checkbox" name="Secondary" value="2" />Secondary</p>
</form>

<script language="javascript" type="text/javascript">
function uncheckSecondary(obj)
        {
            if (obj.checked == true)
            {
                document.getElementById("Secondary").checked = false;
            }
        }
</script>

Here, when the page loads, the Secondarycheckbox will be checked(I have an if clausethere, but it is not related to my question) , but Mainis not checked. As I check the Maincheckbox, Secondaryis unchecked.

在这里,当页面加载时,将选中辅助复选框(我有一个,但与我的问题无关),但未选中Main。当我检查主要复选框,次要未选中if clause

And also I have another piece of code:

而且我还有另一段代码:

<script language="javascript">
function checkRefresh(value)
{
    document.form1.submit();
}
</script>
<form name="form1" action="testing_page2.php" method="GET">
    <p><input id="Main" type="checkbox" name="Main"  value="1" onClick="this.form.submit();"/>Main</p> 
    <p><input id="Secondary" type="checkbox" name="Secondary" value="2"  onClick="this.form.submit();"/>Secondary</p>

</form>

Here, as I check/uncheck any of checkboxes, the form is submitted.

在这里,当我选中/取消选中任何复选框时,表单已提交。

Edited:

编辑:

What I want is to combine function checkRefresh(value)and function uncheckSecondary(obj), so that when I check Main, the Secondary is unchecked and form is submitted, and vice versa.

我想要的是结合function checkRefresh(value)and function uncheckSecondary(obj),这样当我检查 Main 时,未选中 Secondary 并提交表单,反之亦然

回答by Luigi Siri

Try this piece of code...

试试这段代码...

<script>
function checkRefresh(value)
{
    document.form1.submit();
}    

function uncheck(check)
{
    var prim = document.getElementById("Main");
    var secn = document.getElementById("Secondary");
    if (prim.checked == true && secn.checked == true)
    {
        if(check == 1)
        {
            secn.checked = false;
            checkRefresh();
        }
        else if(check == 2)
        {
            prim.checked = false;
            checkRefresh();
        }
    }

}
</script>

<form name="form1" action="#" method="POST">
    <p><input id="Main" type="checkbox" name="Main"  value="1" onClick="uncheck(1);"/>Main</p> 
    <p><input id="Secondary" type="checkbox" name="Secondary" value="2"  onClick="uncheck(2)"/>Secondary</p>

</form>

It will submit the form only when the condition applies as you stated.

只有当条件如您所说的那样适用时,它才会提交表格。

Welcome to Javascript.

欢迎使用 JavaScript。

回答by Hyman

<form name="form1" action="#" method="POST">
    <p><input id="Main" type="checkbox" name="Main"  value="1" onClick="if (this.checked) document.getElementById('Secondary').checked = false; this.form.submit();"/>Main</p> 
    <p><input id="Secondary" type="checkbox" name="Secondary" value="2" onClick=""/>Secondary</p>
</form>