要存储在变量中的 JQUERY 输入字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8624954/
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
JQUERY input field value to be stored in a variable
提问by user1114409
I am unable to capture the value typed by a user in the textbox into a variable.
我无法将用户在文本框中键入的值捕获到变量中。
I do not want to use <form>
, is there any other way to do this?
我不想使用<form>
,有没有其他方法可以做到这一点?
<html>
<head>
<style>
p { color:blue; margin:8px; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input type="text" id="txt_name" />
<script type="text/javascript">
value = $("#txt_name").val();
$("#dom_element").text(value);
</script>
</body>
</html>
回答by nnnnnn
If you want to get a value that the user types then you need to do so in response to some kind of event. The keyup event occurs (believe it or not) when a user is typing and releases a key. If you trap keyup you can update your variable with every keystroke but you should trap "change" as well to allow for paste and drag'n'drop changes that don't use the keyboard. The "change" event occurs when the user modifies the field and then clicks or tabs out of it.
如果您想获得用户键入的值,那么您需要这样做以响应某种事件。keyup 事件发生(信不信由你)当用户正在键入并释放一个键时。如果您捕获 keyup,您可以在每次击键时更新您的变量,但您也应该捕获“更改”以允许不使用键盘的粘贴和拖放更改。“更改”事件发生在用户修改该字段然后单击或跳出该字段时。
Also, at the moment your value
variable is global, but if all you are using it for is to set the value of another field you don't need it at all:
此外,目前您的value
变量是全局变量,但如果您使用它只是为了设置另一个字段的值,您根本不需要它:
$("#txt_name").on("keyup change", function() {
$("#dom_element").text(this.value);
});
// OR, if you need the variable for some other reason:
$("#txt_name").on("keyup change", function() {
var value = this.value; // omit "var" to make it global
$("#dom_element").text(value);
});
Note that within the event handler function this
will be the dom element so you can and should get its value directly without jQuery.
请注意,在事件处理函数this
中将是 dom 元素,因此您可以并且应该在没有 jQuery 的情况下直接获取其值。
If you're using an old version of jQuery use .bind()
instead of .on()
.
如果您使用的是旧版本的jQuery的使用.bind()
代替.on()
。
回答by Al-Mothafar
you mean like this?
你的意思是这样?
Script:
脚本:
jQuery(function(){
$("#txt_name").keypress(function() {
var value = $("#txt_name").val();
$("#myDiv").text(value);
});
});
HTML:
HTML:
<form>
<fieldset>
<input id="txt_name" type="text" value="Hello World" />
</fieldset>
</form>
<div id="myDiv"></div>
I hope that help you .
我希望对你有帮助。
回答by Ashkan Mobayen Khiabani
<script>
$('#txt_name').keyup(function(){
value = $("#txt_name").val();
$("#dom_element").val(value);
});
</script>
this is from api.jquery.com:
这是来自api.jquery.com:
The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.
As of jQuery 1.4, the .text() method returns the value of text and CDATA nodes as well as element nodes.
.text() 方法不能用于表单输入或脚本。要设置或获取 input 或 textarea 元素的文本值,请使用 .val() 方法。要获取脚本元素的值,请使用 .html() 方法。
从 jQuery 1.4 开始, .text() 方法返回文本和 CDATA 节点以及元素节点的值。