How to Use jquery to obtain values in a form Element

Source: Internet
Author: User

There are many ways to get the value of a form element in jquery. I will introduce in detail the examples and methods for getting the value of a form element in jquery, for more information, see.

Such as html

The Code is as follows: Copy code

<Input type = "radio" value = "1" name = "radio_name"/> 1
<Input type = "radio" value = "2" name = "radio_name"/> 2
<Input type = "radio" value = "3" name = "radio_name"/> 3

Jquery

$ ("Input [name = 'Radio _ name']: checked"). val ()


Some friends may not understand it. Let's take a look at val usage now.

Val ()


Obtains the current value of the First Matching Element.

In jQuery 1.2, the value of any element can be returned. Including select. If multiple values are selected, an array containing the selected values is returned.

Return Value

String, Array


/* Obtain the value of TEXT. AREATEXT */

The Code is as follows: Copy code

Var textval = $ ("# text_id"). attr ("value ");
// Or
Var textval = $ ("# text_id"). val ();

JQuery reads the value of the input element:

The Code is as follows: Copy code

<Input type = "text" id = "keyword"/>

The conventional approach is: $ ("# keyword "). the value cannot be obtained, because $ ('# keydord') is not an element but a jquery object, so you should use:

The Code is as follows: Copy code

Var inputValue = $ ("# keyword"). val ();

Because the first element in the Qjuery object is a DOM object, you can also set the value as follows:

The Code is as follows: Copy code

Var inputValue = $ ("# keyword") [0]. value;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.