Example code of the val () form value assignment in Jquery

Source: Internet
Author: User

JQuery provides a powerful val () method to process value-related operations.
Obtain the values of a single select statement and the values of multiple select statements.
Copy codeThe Code is as follows:
HTML code:
<P> </p> <br/>
<Select id = "single">
<Option> Single </option>
<Option> Single2 </option>
</Select>
<Select id = "multiple" multiple = "multiple">
<Option selected = "selected"> Multiple </option>
<Option> Multiple2 </option>
<Option selected = "selected"> Multiple3 </option>
</Select>
JQuery code:
$ ("P"). append (
"<B> Single: </B>" + $ ("# single"). val () +
"<B> Multiple: </B>" + $ ("# multiple"). val (). join (",")
);
Result:
[<P> <B> Single: </B> Single <B> Multiple: </B> Multiple, Multiple3 </p>]

Get the value in the text box
HTML code:
Copy codeThe Code is as follows:
<Input type = "text" value = "some text"/>
JQuery code:
$ ("Input"). val ();

Result:
Some text
The following is how var () can get/assign the value of the form element.
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> val () method </title>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type = "text/javascript"> </script>
<Script language = "javascript">
$ (Function (){
$ ("Input [type = button]"). click (function (){
Var sValue = $ (this). val (); // obtain the value of the button first.
$ ("Input [type = text]"). val (sValue); // assign it to the text box
});
});
</Script>
</Head>
<Body>
<P>
<Input type = "button" value = "Feed">
<Input type = "button" value = "the">
<Input type = "button" value = "Input">
</P>
<P>
<Input type = "text" value = "click a button">
</P>
</Body>
</Html>

Note: var data = $ ("# myId"). val ();
If myid does not exist, data is undefined.

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.