使用 jQuery 获取文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19063996/
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
Getting textbox's value using jQuery
提问by Devesh Agrawal
I am creating a login form dynamically, using jQuery and AJAX response. Form is created and displaying properly. But, I'm not able to read the form data.
我正在使用 jQuery 和 AJAX 响应动态创建登录表单。表单已创建并正确显示。但是,我无法读取表单数据。
This is my code:
这是我的代码:
$(document).ready(function(){
$('#proper-form').on( 'click', '#submit-button', function() { // loginForm is submitted
alert ("here");
var username = $('#email').attr('value'); // get username
var password = $('#password').attr('value'); // get password
alert (password);
alert (username);
// code for form submit using ajax
});
});
It's alerting undefined undefined
for both username and password.
它会提醒undefined undefined
用户名和密码。
Can anyone help me in figuring out what's wrong here?
谁能帮我弄清楚这里出了什么问题?
回答by Suresh Atta
Just use the val()
function, to get the value:
只需使用该val()
函数即可获取值:
var username = $('#email').val();
var password = $('#password').val();
To set the value use val(value)
function:
设置值使用val(value)
函数:
$('#email').val('some new value');
回答by Arun P Johny
You need to use .val() to read the value of an input element, not attribute
您需要使用 .val() 来读取输入元素的值,而不是属性
var username = $('#email').val();
回答by Uriah Sanders
There are already good answers here, but I thought you should understand why using .attr() doesn't work, even though value is indeed an attribute of the input field.
这里已经有很好的答案,但我认为您应该理解为什么使用 .attr() 不起作用,即使 value 确实是输入字段的属性。
When you use $('#email').attr('value');, this is equivalent to something like:
当您使用 $('#email').attr('value'); 时,这相当于:
input.getAttribute('value'),
which would return "something" for:
这将返回“东西”:
<input type="text"value="something"/>
regardless of what the user typed in.
无论用户输入什么。
However, using
$('#email').val()
is equivalent to something like:
input.value
which would instead return whatever the user actually typed into the field.
但是, using
$('#email').val()
等效于类似的内容:
input.value
它会返回用户实际在字段中输入的任何内容。
In this case, you don't have a value="" implicitly set: you just want what the user typed in.
在这种情况下,您没有隐式设置 value="":您只需要用户输入的内容。
Therefore, use .val()
.
因此,使用.val()
.
回答by Sudhir chaudhary
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#test").click(function(){
//textbox
var first_name = $("#first_name").val();
alert(first_name);
});
});
</script>
<div>username <input type="text" id="first_name" class="first_name" name="first_name" placeholder="type username"/>
<a href="javascript:void(0)" id="test" class="shortc-button small blue">Get Data</a></div>
For more details visit : http://www.codingprogrammer.com/tutorialdemo/jquery-tutorial/get-value-of-textbox-in-jquery-example/
有关更多详细信息,请访问:http: //www.codingprogrammer.com/tutorialdemo/jquery-tutorial/get-value-of-textbox-in-jquery-example/