简体   繁体   English

如何获取输入以及何时按下提交按钮将其置于变量中?

[英]How to take an input and when the submit button is pressed put it into a variable?

Lets say I got some HTML like this 可以说我得到了一些像这样的HTML

<input type="text" placeholder="Enter Mineral">
<input type = "submit">

I have no idea how to put what is in the text into a javascript variable when the submit button is pressed. 我不知道如何在按下提交按钮时将文本中的内容放入javascript变量中。 I'll use jQuery if that makes it easier. 我会使用jQuery,如果这样更容易。

Use .val() to fetch the value of the input field. 使用.val()来获取输入字段的值。

$("input[type=submit]").click(function(e){

   e.preventDefault(); // <-------- To stop form submission
   var txt = $("input[type=text]").val(); // <----- Fetch value of input field
   console.log(txt) // <----------- Check console has the value of the input field.

});

暂无
暂无

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

相关问题 如何从一个javascript变量获取一个值,通过一个按钮用作一个php输入变量,没有或没有提交按钮? - how to take a Value from a javascript variable, to be used as a php input variable via a button, without or not a submit button? 按下提交按钮后如何在FireBug中停止? - How to stop in FireBug when submit button is pressed? 按下提交按钮时,使用javascript获取特定表单的输入值 - Get the input value of a specific form with javascript when the submit button is pressed Node.js:如何在按下“提交”按钮时将公式的输入值写入JSON文件? - Node.js: How to write input values of a formular into a JSON file when the submit button is pressed? 如何防止按下提交按钮时React回发 - How to prevent React from posting back when submit button is pressed 如何在按下按钮时从用户复制输入字段并将其保存到变量 - How to copy the input field from a user when a button is pressed and save it to a variable 按下提交按钮时如何更改表单的动作|| 使提交按钮只能单击一次? - how to change the action of a form when the submit button is pressed || make the submit button clickable only once? 按下按钮时尝试将表单输入的值放入另一个表单输入中 - Trying to put value of a form input in another form input when a button is pressed 如何获取输入值并将其放入angularJS中的变量? - How can I take a input value and put into a variable in angularJS? 按下提交时更改输入的样式 - Change the style of an input when submit is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM