[英]uncheck one checkbox as another is checked and submit the form
I am trying to combine two javascript
functions 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 Secondary checkbox will be checked (I have an if clause
there, but it is not related to my question) , but Main is not checked.在这里,当页面加载时,将选中辅助复选框(我在那里有一个
if clause
,但它与我的问题无关),但未选中Main 。 As I check the Main checkbox, Secondary is unchecked .当我选中Main复选框时,未选中Secondary 。
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)
和function uncheckSecondary(obj)
,这样当我检查 Main 时,Secondary 未选中并提交表单,反之亦然。
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。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.