1

有两种选择;英语或非英语。当您不选择其中任何一个或同时选择两者时,它应该给出错误但它没有给出。你能帮忙吗?谢谢。ps:即使我将其设置为条款和条件的复选框,警报也不起作用。

JavaScript

function onFormSubmit(form_element) {

        if (form_element.click.checked == false)
                {
                alert("Please check language");
                return false;
                }
    return true;
}

HTML

<form onsubmit="return onFormSubmit(this)">
        Language: <input type="checkbox" name="click" value="english">english
</form>
4

4 回答 4

1

条件应该...

if ((form_element.click_1.checked == true && form_element.click_2.checked == true) || (form_element.click_1.checked == false && form_element.click_2.checked == false))

编辑

这是我用的。

<script>
function onFormSubmit(form_element) {

        if ((form_element.click_1.checked == true && form_element.click_2.checked == true) || (form_element.click_1.checked == false && form_element.click_2.checked == false))
                {
                alert("Please check language");
                return false;
                }
    return true;
}
</script>


<form onsubmit="return onFormSubmit(this)">
        Language: <input type="checkbox" name="click_1" value="english">english
                  <input type="checkbox" name="click_2" value="non english">non english</p>
<input type="submit">
</form>
于 2012-04-15T14:10:46.763 回答
0

问题是“onFormSubmit(this)”——当事件被调用时,“this”不会引用表单。如果您从文档中查找表单,它将正常工作。

<!doctype html>
<html>
<head>
<script type="text/javascript">
function validateForm() {
        var form = document.getElementById("myForm");
        if (!form.language.checked) {
                alert("Please check language");
                return false;
        }
        return true;
}
</script>
</head>
<body>
<form id="myForm" onsubmit="return validateForm()">
        Language: <input type="checkbox" name="language">English</input><br />
        <input type="submit" />
</form>
</body>
</html>
于 2012-04-15T14:20:47.570 回答
0
<form onsubmit="return onFormSubmit(this)">
        Language: <input type="checkbox" name="click_1" value="english">english
                  <input type="checkbox" name="click_2" value="non english">non english</p>
        <input type="submit" />
</form>
<script type="text/javascript">
function onFormSubmit(form_element) {
  if ((form_element.click_1.checked == true && form_element.click_2.checked == true)) {
      alert("Please check only one choice!");
      return false;
  } else if ((form_element.click_1.checked == false && form_element.click_2.checked == false)) {
      alert("Please check your choice!");
      return false;
    }
    return true;
}
</script>
​

http://jsfiddle.net/ocanal/3AWUp/

于 2012-04-15T14:22:51.743 回答
0

我在每个元素的 HTML 和 id 中添加了一个提交按钮

HTML

<form id="myForm">
        Language: <input id="english" type="checkbox" name="click_1" value="english">english
                  <input id="nonEnglish" type="checkbox" name="click_2" value="non english">non english</p>
<button type="submit">Submit</button>
</form>

我将一个函数附加到执行验证的 onsubmit 事件。Javascript

document.getElementById("myForm").onsubmit =
function () {
        if ((document.getElementById("english").checked == false && document.getElementById("nonEnglish").checked == false) || (document.getElementById("english").checked == true && document.getElementById("nonEnglish").checked == true))
                {
                alert("Please check language");
                return false;
                }
    return true;
}

演示:http: //jsfiddle.net/j3J4E/

于 2012-04-15T14:23:32.747 回答