admin管理员组

文章数量:1417719

I am trying to bine 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 bine function checkRefresh(value) and function uncheckSecondary(obj) , so that when I check Main, the Secondary is unchecked and form is submitted, and vice versa.

I am trying to bine 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 bine function checkRefresh(value) and function uncheckSecondary(obj) , so that when I check Main, the Secondary is unchecked and form is submitted, and vice versa.

Share Improve this question edited Aug 2, 2020 at 20:24 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Apr 18, 2013 at 21:13 user2170133user2170133 793 gold badges3 silver badges11 bronze badges 1
  • Try the new code i just added... – Luigi Siri Commented Apr 19, 2013 at 14:23
Add a ment  | 

2 Answers 2

Reset to default 2

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.

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

本文标签: javascriptuncheck one checkbox as another is checked and submit the formStack Overflow