如何使用jQuery获取文本框的值?

时间:2022-11-27 07:58:37

I can get the element like this $("#txtEmail") but I'm not sure how to get the actual value.

我可以获得像$(“#txtEmail”)这样的元素,但我不确定如何获得实际值。

8 个解决方案

#1


716  

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")

#2


73  

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”)返回类型为 的元素,而您可能没有这些元素。

See here at the jQuery documentation.

参见这里的jQuery文档。

Also jQuery val() method.

还jQuery瓦尔()方法。

#3


22  

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();

#4


9  

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/jqueryplugins/jqueryplugin -validation/,它还附带一个电子邮件规则。

#5


6  

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代码中!

#6


5  

Use the .val() method to get the actual value of the element you need.

使用.val()方法获取所需元素的实际值。

#7


4  

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来获取。

#8


2  

By Using

通过使用

$("#txtEmail").val()

you get the actual value of the element

得到元素的实际值

#1


716  

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")

#2


73  

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”)返回类型为 的元素,而您可能没有这些元素。

See here at the jQuery documentation.

参见这里的jQuery文档。

Also jQuery val() method.

还jQuery瓦尔()方法。

#3


22  

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();

#4


9  

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/jqueryplugins/jqueryplugin -validation/,它还附带一个电子邮件规则。

#5


6  

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代码中!

#6


5  

Use the .val() method to get the actual value of the element you need.

使用.val()方法获取所需元素的实际值。

#7


4  

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来获取。

#8


2  

By Using

通过使用

$("#txtEmail").val()

you get the actual value of the element

得到元素的实际值