简体   繁体   中英

uncheck one checkbox as another is checked and submit the form

I am trying to combine two javascript functions into one function.

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 Secondary checkbox will be checked (I have an if clause there, but it is not related to my question) , but Main is not checked. As I check the Main checkbox, Secondary is unchecked .

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM