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.