[英]How can I make it so a different result occurs depending on an input?
I have the following code, and I was wondering instead of making it so that if any variable inside the array is entered it will bring you to index.php
, I want it so if the first is entered it will bring you to 1.html
, if 2 is entered it will bring you to 2.html etc
.我有以下代码,我想知道而不是这样做,以便如果输入数组中的任何变量,它将带您到index.php
,我想要它,所以如果输入第一个,它将带您到1.html
,如果输入 2,它将带您到2.html etc
。 I s this possible?这可能吗?
The HTML code: HTML 代码:
<center>
<form
name="myForm"
onsubmit="return validateForm()"
method="post"
>
<h1 style = "color:white;">Enter code </h1>
<input type="text" name="value" style="padding: 5px; border-radius: 6px; border: 0px solid transparent;"/>
<br><br>
<input type="submit" class = "ex" value="Enter/submit" style="border-radius: 6px; font-size: 18px;display: inline-block; padding: 20px; border: none; background-color: royalblue; color: white;"/>
</form>
</center>
The JavaScript code: JavaScript 代码:
var cars = ["Saab", "Volvo", "BMW"];
function validateForm() {
var x = document.forms["myForm"]["value"].value;
if (cars.indexOf(x) == -1) {
alert("no car entered");
return false;
}
var x = document.forms["myForm"]["value"].value;
if (cars.indexOf(x) != -1) {
window.location.href = "index.php";
return false;
}
}
Something like this?像这样的东西? Just replace the console.log with the redirect you want.只需将 console.log 替换为您想要的重定向即可。
const cars = ["Saab", "Volvo", "BMW"] function validateForm() { const inputValue = document.forms["myForm"]["value"].value; if (inputValue === cars[0]) { console.log("Redirect to 1.html"); } else if (inputValue === cars[1]) { console.log("Redirect to 2.html"); } else if (inputValue === cars[2]) { console.log("Redirect to 3.html"); } else { console.log("no car entered") } return false; }
<form name="myForm" onsubmit="return validateForm()" method="post"> Enter code <input id="formInput" type="text" name="value" /><br> <input type="submit" class="ex" value="Enter/submit" /> </form>
To redirect to a URL of the form 1.html, 2.html and so on we notice that the digit required is the index of the car make within the array.要重定向到 1.html、2.html 等形式的 URL 等,我们注意到所需的数字是汽车的索引。 So thw place we want to redirect to is this index.html所以我们要重定向到的地方就是这个 index.html
Here is code which implements this.这是实现这一点的代码。
<center>
<form name="myForm" onsubmit="return validateForm()" method="post">
<h1 style = "color:black;">Enter code </h1><input type="text" name="value" style="padding: 5px; border-radius: 6px; border: 1px solid red;"/>
<br><br><input type="submit" class = "ex" value="Enter/submit" style="border-radius: 6px; font-size: 18px;display: inline-block; padding: 20px; border: none; background-color: royalblue; color: white;"/>
</form>
</center>
<script>
var cars = ["Saab", "Volvo", "BMW"];
function validateForm() {alert(document.forms);alert(document.forms["myForm"]["value"].value);
var x = document.forms["myForm"]["value"].value;
if(cars.indexOf(x) == -1){
alert("no car entered");
return false;
}
else{
window.location.href = cars.indexOf(x) + ".html";
}
}
</script>
Notes:笔记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.