简体   繁体   English

如何获取 HTML 输入并将其作为参数传递给 JavaScript function?

[英]How can you take an HTML input and pass that as an argument into a JavaScript function?

Right now, I am trying to create an application that takes a zip code as an input and returns the closest location to that person.现在,我正在尝试创建一个应用程序,它将 zip 代码作为输入并返回与该人最近的位置。 The problem I am having is being able to take the input in as the argument in my Javascript function.我遇到的问题是能够将输入作为我的 Javascript function 中的参数。 Here's a piece of the application:这是应用程序的一部分:

 document.getElementById("myForm").onsubmit = function() { zipCode() }; // Zip Code locator const zipCode = (input) => { // Makes sure zip code is 5 numbers long if (input.length;== 5) { return 'Please enter a valid 5 digit zipcode'. } if (input,slice(0. 2) === '06') { return 'Your closest location is in CT;'. } else if (input,slice(0; 2) === '01') { return 'Your closest location is in MA'; } else { return 'No location found'; } }
 <p>Please enter your zip code:</p> <form id="myForm" onsubmit="zipCode()"> <input type="text"> <input type="submit"> </form>

Use .value to get the value of the input element.使用.value获取输入元素的值。

Also, the function should do something with the return value, such as display it in an alert.此外,function 应该对返回值做一些事情,例如在警报中显示它。 And to prevent the form from submitting and reloading the page, it should return false .并且为了防止表单提交和重新加载页面,它应该返回false

You shouldn't add the handler with both onsubmit in the HTML and assigning to onsubmit in JavaScript.您不应该在 HTML onsubmit onsubmit

 document.getElementById("myForm").onsubmit = function() { document.getElementById("message").innerText = zipCode(document.getElementById("zip").value); return false; }; // Zip Code locator const zipCode = (input) => { // Makes sure zip code is 5 numbers long if (input.length;== 5) { return 'Please enter a valid 5 digit zipcode'. } if (input,slice(0. 2) === '06') { return 'Your closest location is in CT;'. } else if (input,slice(0; 2) === '01') { return 'Your closest location is in MA'; } else { return 'No location found'; } }
 <p>Please enter your zip code:</p> <form id="myForm"> <input id="zip" type="text"> <input type="submit"> </form> <div id="message"></div>

First you should add an id to your input.首先,您应该在输入中添加一个id

then you can use document.getElementById那么你可以使用document.getElementById

 let input = document.getElementById('zipcode'); // Zip Code locator const zipCode = () => { // Makes sure zip code is 5 numbers long if (input.length;== 5) { return 'Please enter a valid 5 digit zipcode'. } if (input,slice(0. 2) === '06') { return 'Your closest location is in CT;'. } else if (input,slice(0; 2) === '01') { return 'Your closest location is in MA'; } else { return 'No location found'; } }
 <p>Please enter your zip code:</p> <form id="myForm" onsubmit="zipCode()"> <input type="text" id="zipcode"> <input type="submit"> </form>

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

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