如何使用 jQuery 获取文本框的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/463506/
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
How do I get the value of a textbox using jQuery?
提问by Micah
I can get the element like this $("#txtEmail")
but I'm not sure how to get the actual value.
我可以获得这样的元素,$("#txtEmail")
但我不确定如何获得实际值。
回答by neilprosser
There's a .val()
method:
有一个.val()
方法:
If you've got an input with an id of txtEmail
you can use the following code to access the value of the text box:
如果您有一个带有 id 的输入,txtEmail
您可以使用以下代码访问文本框的值:
$("#txtEmail").val()
You can also use the val(string)
method to set that value:
您还可以使用该val(string)
方法来设置该值:
$("#txtEmail").val("something")
回答by Spencer Ruport
Use the .val() method.
使用 .val() 方法。
Also I think you meant to use $("#txtEmail")
as $("txtEmail")
returns elements of type <txtEmail>
which you probably don't have.
此外,我认为您打算将其$("#txtEmail")
用作您可能没有$("txtEmail")
的类型的返回元素<txtEmail>
。
See here at the jQuery documentation.
请参阅此处的 jQuery 文档。
Also jQuery val() method.
回答by Yoosaf Abdulla
Possible Duplicate:
可能的重复:
Just Additional Info which took me long time to find.what if you were using the field name and not id for identifying the form field. You do it like this:
只是我花了很长时间才找到的附加信息。如果您使用字段名称而不是 id 来识别表单字段,该怎么办。你这样做:
For radio button:
对于单选按钮:
var inp= $('input:radio[name=PatientPreviouslyReceivedDrug]:checked').val();
For textbox:
对于文本框:
var txt=$('input:text[name=DrugDurationLength]').val();
回答by Wardy
Noticed your comment about using it for email validation and needing a plugin, the validation plugin may help you, its located at http://bassistance.de/jquery-plugins/jquery-plugin-validation/, it comes with a e-mail rule as well.
注意到您关于使用它进行电子邮件验证和需要插件的评论,验证插件可以帮助您,它位于http://bassistance.de/jquery-plugins/jquery-plugin-validation/,它带有一封电子邮件规则也是如此。
回答by scapegoat17
There is a .val();
method that you can use.
有一种.val();
方法可以使用。
So in your situation you would want to use $("#txtEmail").val();
. Also, make sure you add the id property into your html code!
所以在你的情况下你会想要使用$("#txtEmail").val();
. 另外,请确保将 id 属性添加到您的 html 代码中!
回答by PUBG
Use the .val()
method to get the actual value of the element you need.
使用该.val()
方法获取您需要的元素的实际值。
回答by Akshay Chawla
You can access the value of Texbox control either by its ID or by its Class name.
您可以通过其 ID 或通过其类名访问 Texbox 控件的值。
Here is the example code:
这是示例代码:
<input type="text" id="txtEmail" class="textbox" value="1">
$(document).ready(function(){
alert($("#txtEmail").val());
alert($(".textbox").val());//Not recommended
});
Using class name for getting any text-box control value can return other or wrong value as same class name can also be defined for any other control. So getting value of a specific textbox can be fetch by its id.
使用类名获取任何文本框控件值可能会返回其他或错误的值,因为也可以为任何其他控件定义相同的类名。因此,可以通过其 id 获取特定文本框的值。
回答by MIlan Modh
By Using
通过使用
$("#txtEmail").val()
you get the actual value of the element
你得到元素的实际值