Get the value of the form control by using jQuery to operate the DOM, and use the jquerydom form control.

Source: Internet
Author: User

Get the value of the form control by using jQuery to operate the DOM, and use the jquerydom form control.

This document describes how to use jQuery to operate DOM to obtain the value of a form control. Share it with you for your reference. The specific analysis is as follows:

The biggest difference between HTML attributes and DOM attributes is probably the value of the Form Control. For example, the value attribute of the text box in the DOM is called defaultValue, and there is no value attribute in the DOM. For the select element, the value of the option is usually obtained through the selectedIndex attribute in the DOM or by the selected Attribute of the Option element.

Because of these differences, it is best not to use the. attr () method when obtaining and setting the value of the Form Control. For the option list, it is best not to use the. prop () method. Then, we recommend that you use the. val () method provided by jQuery:

Copy codeThe Code is as follows: // get the current value of the text input box
Var inputValue = $ ('# my-input'). val ();
// Obtain the current value of the Option List
Var selectValue = $ ('# my-select'). val ();
8. Set the value of the radio list
$ ('# My-single-select'). val ('value3 ');
/^ Set the value of the multiple-choice list
$ ('# My-multi-select'). val (['value1', 'value2']);

Like. attr () and. prop ()-, the. val () method can also accept a function as its setter parameter. With this multi-purpose. val () method, you will feel more efficient when using jQuery for Web development.

I hope this article will help you with jQuery programming.

Related Article

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.