[英]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.