简体   繁体   English

无法使此表单在HTML5上运行

[英]Can't get this form to work on HTML5

sorry for what might be a noob question.. I've been fiddling with this form for some time now but I can't seem to make it work for some reason. 抱歉,这可能是一个菜鸟问题。.我已经摆弄了一段时间,但是由于某种原因,我似乎无法使其正常工作。 The colour changes work but the submit button simply does not lead to the link.. 颜色更改有效,但是提交按钮根本无法导致链接。

<center>
    <form name="form1" method="POST">
        <input id="codebox1" type="text" onchange="checkFilled1();" />
        <input id="codebox2" type="text" onchange="checkFilled2();" />
        <input id="codebox3" type="text" onchange="checkFilled3();" />
        <br>
        <br>
        <input id="button1" type="submit" value="Submit" onClick="testResults(this.form)" />
    </form>
</center>

And here are the functions: 这里是函数:

<script>
function checkFilled1() {
    var inputVal = document.getElementById("codebox1");
    if (inputVal.value == "1234") {
        inputVal.style.backgroundColor = "lightgreen";
    } else {
        inputVal.style.backgroundColor = "red";
    }
}
checkFilled1();
</script>

<script>
function checkFilled2() {
    var inputVal = document.getElementById("codebox2");
    if (inputVal.value == "1234") {
        inputVal.style.backgroundColor = "lightgreen";
    } else {
        inputVal.style.backgroundColor = "red";
    }
}

checkFilled2();
</script>

<script>
function checkFilled3() {
    var inputVal = document.getElementById("codebox3");
    if (inputVal.value == "1234") {
        inputVal.style.backgroundColor = "lightgreen";
    } else {
        inputVal.style.backgroundColor = "red";
    }
}

checkFilled3();
</script>

<script>
function testResults() {
    var inputVal1 = document.getElementById("codebox1");
    var inputVal2 = document.getElementById("codebox2");
    var inputVal3 = document.getElementById("codebox3");
    if (inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234") {
        window.open("http://google.com", "_parent");
    } else {

    }
}

testResults();
</script>

Thanks for the help! 谢谢您的帮助!

You're calling the method, testResults which does not accept any parameters. 您正在调用的方法testResults不接受任何参数。 Change the submit button markup to the following and the testResults method will be invoked. 将提交按钮标记更改为以下内容,将调用testResults方法。

<input id="button1" type="submit" value="Submit" onClick="testResults()" />

No parameters. 没有参数。

Change this: 更改此:

<form name="form1" method="POST">

[...]

<input id="button1" type="submit" value="Submit" onClick="testResults(this.form)" />

to this: 对此:

<form name="form1" onSubmit="testResults()" method="POST">

[...]

<input id="button1" type="submit" value="Submit" />

=== ===

Refactored Version 重构版本

 var codeboxes = document.getElementsByClassName('codebox'); function testResults() { var correctValues = 0; for (var i = 0; i < codeboxes.length; i++) { if (codeboxes[i].value === '1234') { correctValues++; } } if (correctValues === codeboxes.length) { window.alert('You have entered all ' + codeboxes.length + ' values correctly.'); } else { window.alert('You have not entered all ' + codeboxes.length + ' values correctly.'); } } 
 <form name="form1" onSubmit="testResults()" method="POST"> <input class="codebox" type="text" /> <input class="codebox" type="text" /> <input class="codebox" type="text" /> <input id="button1" type="submit" value="Submit" /> </form> 

You have a syntax error in your JavaScript. 您的JavaScript中有语法错误。 Open up your browser tools console and see: 打开浏览器工具控制台,然后查看:

Unexpected token && 意外的标记 &&

You need to wrap the whole if condition in parentheses, like this: 您需要将整个if条件包装在括号中,如下所示:

if ((inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234")) {
window.open("http://www.example.com", "_parent");

That said, this won't submit the data in the form. 也就是说,这不会在表单中提交数据。 It will just open the give URL if the conditions are true — which is what you asked in your question. 如果条件成立,它将只打开给定URL,这就是您在问题中所提出的。

 function checkFilled(inp) { var inputVal = inp; if (inputVal.value == "1234") { inputVal.style.backgroundColor = "lightgreen"; } else { inputVal.style.backgroundColor = "red"; } } function testResults() { var inputVal1 = document.getElementById("codebox1"); var inputVal2 = document.getElementById("codebox2"); var inputVal3 = document.getElementById("codebox3"); if ((inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234")) { alert('Here comes window opening...'); //window.open("http://google.com", "_blank"); } else { alert('Error ...'); } } //testResults(); 
 <form name="form1" method="POST"> <input id="codebox1" type="text" onchange="checkFilled(this);" /> <input id="codebox2" type="text" onchange="checkFilled(this);" /> <input id="codebox3" type="text" onchange="checkFilled(this);" /> <br> <br> <input id="button1" type="submit" value="Submit" onClick="testResults(this.form)" /> </form> 

you've done one mistake at testResults() function, missing () at if ... see the snippet (with a little optimalization) 您在testResults()函数中犯了一个错误,如果...则缺少了()...请参见代码段(稍作优化)

Edit your form tag as below, 如下编辑表单标签,

<form name="form1" method="POST" onSubmit="testResults()">

Edit your submit button as below, 如下编辑您的提交按钮,

<input id="button1" type="submit" value="Submit"/>

Edit the if statement as follow, 如下编辑if语句,

if((inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234"))
{
     window.open("http://www.google.com", "_parent");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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