简体   繁体   English

我怎样才能使它根据输入产生不同的结果?

[英]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:笔记:

  • center is deprecated - use CSS instead center 已弃用 - 改用 CSS
  • the code in the question declared x twice问题中的代码两次声明 x
  • if...else is used above instead of testing the condition twice if...else 在上面使用,而不是两次测试条件
  • there seems no point in the final return statement since the user is being redirected由于正在重定向用户,因此最终的 return 语句似乎没有意义
  • the border of the input element was set to 0. For this test it has been put at 1px and color red so it can be seen输入元素的边框设置为 0。对于这个测试,它被设置为 1px,颜色为红色,因此可以看到

暂无
暂无

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

相关问题 我怎样才能让 javascript 检测不同的显示尺寸,然后根据尺寸转到网站? - How can I make it so javascript detects different display sizes and then goes to a website depending on the size? 使用相同输入数据的网站javascript代码如何根据访问者计算出不同的结果? - how can website javascript code using same input data compute a different result depending on visitor? 我如何进行输入,以便 javacscipt 有 1 个字母和 7 个数字 - how can I make an input so there is 1 letter and 7 numbers with javacscipt 我怎样才能做到这一点,当用户输入一个字母时,输入会更改为下一个输入字段等等? - How can I make it so that when a user types a letter the input would change to the next input field and so on? 如何让按钮将我的页面重定向到另一个页面(取决于输入) - How can I make a button redirect my page to another page (depending on the input) p5.j​​s:如何根据HTML页面使草图加载不同的.txt文件? - p5.js: How can I make my sketch load a different .txt file depending on the HTML page? 如何让 discord 机器人检查消息的内容并根据作者回复不同的消息? - How can I make a discord bot check the contents of a message and reply with a different message depending on the author? 如何根据 Controller 的响应在视图中显示完全不同的模态? - How can I make a completely different Modal appear in the View, depending on the response from the Controller? 当我创建一个输入框时,我怎样才能让它输出我在输入框中输入了多少? - When i created a input box, how can i make it so it outputs how much I put into the input box? 如何根据另一个输入angularJS验证输入? - How can I validate a input depending another input angularJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM