javascript 如何获取用户输入到传递给javascript函数的html文本表单字段的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7289003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:36:07  来源:igfitidea点击:

How to get value user entered into html text form field passed to javascript function?

javascripthtmlfunctioninputtextfield

提问by user671891

I'm trying to create my own anti-spam filter by generating random numbers that the user has to enter into a text input form field, if it's correct they proceed to the next page, if not it displays an error that the number entered is incorrect.

我正在尝试通过生成用户必须在文本输入表单字段中输入的随机数来创建我自己的反垃圾邮件过滤器,如果正确,他们将进入下一页,如果不正确,则显示输入的数字是错误的不正确。

I have the following code for my html form:

我的 html 表单有以下代码:

 <form name="input" action="nextpage.html" onSubmit="return checkUserInput()" method="get">
 Number: 

 <input type="text" name="user" />
 <input type="submit" value="Submit" />

 </form>

The checkUserInput() is a javascript function which will take the user's input, compare it to the random number that was displayed to make sure they're the same, then return true, or false if the two numbers are not the same.

checkUserInput() 是一个 javascript 函数,它将接受用户的输入,将其与显示的随机数进行比较以确保它们相同,然后返回 true,如果两个数字不相同则返回 false。

How do I get the input that the user entered into the text field form and pass it to the checkUserInput() function. I was thinking it would be "return checkUserInput(INPUT)" but I'm not sure how to properly pass the user's input to the function.

如何获取用户在文本字段表单中输入的输入并将其传递给 checkUserInput() 函数。我以为它会是“返回 checkUserInput(INPUT)”,但我不确定如何正确地将用户的输入传递给函数。

A secondary issue I have is I'd like the page to refresh with a different random number and an error message to be displayed if the user clicks "Submit" when the numbers aren't the same. Right now the only action that exists is: action="nextpage.html" but I want an option for when checkUserInput() returns false. Is there a way to have two different actions based on whether checkUserInput() returns true or false?

我遇到的第二个问题是,如果用户在数字不同时单击“提交”,我希望使用不同的随机数刷新页面并显示错误消息。现在唯一存在的操作是: action="nextpage.html" 但我想要一个选项,用于何时 checkUserInput() 返回 false。有没有办法根据 checkUserInput() 返回 true 还是 false 来执行两种不同的操作?

Thanks, Joe

谢谢,乔

Here's also my checkUserInput() function:

这也是我的 checkUserInput() 函数:

 var whatevertheyentered;

 function checkUserInput(whatevertheyentered){

 if(whatevertheyentered == randomnumber){
      return true;
 }
 else {
      return false;
 }

 }

回答by Nick Shaw

You could access it using document.form[0].user.value(assuming it's the fist form on the page).

您可以使用document.form[0].user.value(假设它是页面上的第一个表单)访问它。

Or give the "user" input box an ID and find it using document.getElementById("theId").

或者给“用户”输入框一个 ID 并使用document.getElementById("theId").

Do both within your checkUserInputfunction.

在你的checkUserInput职能范围内做到这两点。