jquery Learning two attribute text and value (text,val) _jquery

Source: Internet
Author: User

Text ()

Gets the contents of all matching elements.
The result is text that is combined with the text content contained by all matching elements. This method works for both HTML and XML documents.
Get the text contents to all matched elements.
The result are a string that contains the combined text contents to all matched elements. This method works on both HTML and XML documents.

return value

String

Example

HTML Code:

<p><b>Test</b> paragraph.</p><p>paraparagraph</p>

JQuery Code:

$ ("P"). text ();

Results:

Test Paragraph.paraparagraph
--------------------------------------------------------------------------------------------- ---------------------------------
text (val)
Set text content for all matching elements
Set the text contents of all matched elements.
Similar to HTML (), but escapes HTML (replace "<" and ">" with their HTML entitie s).
return value

jquery

parameter

Val (String): Text

example

HTML code for setting element content:

<p> ; Test paragraph.</p>

jQuery code:

$ ("P"). Text ( "<b>Some</b> new text.");

result:

[<p><b>Some</b> new text.< ;/p>]
--------------------------------------------------------------------------------------------------------------- ---------------

Val ()

Gets the current value of the first matching element.
In JQuery 1.2, you can return the value of any element. Include SELECT. If multiple selections, an array is returned that contains the selected value.
Get the content of the matched
In JQuery 1.2, a value are now returned for all elements, including selects. For multiple selects an array of values are returned.

return value

String,array

Example

Gets the value of a single select and the value of a multiple select.

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

Results:

[<p><b>single:</b>single<b>multiple:</b>multiple, Multiple3</p>]

Get the value in a text box

HTML Code:

<input type= "text" value= "some text"/>

JQuery Code:

$ ("input"). Val ();

Results:

Some text
--------------------------------------------------------------------------------------------------------------- ---------------

Val (Val)

Sets the value of each matching element.
In JQuery 1.2, this can also assign a value to a select component
Set The value attribute of every matched element.
In JQuery 1.2, this is also able to set the value of SELECT elements, but selecting the appropriate options.

return value

Jquery

Parameters

Val (String): The value to set.

Example

Set the value of a text box

HTML Code:

<input type= "Text"/>

JQuery Code:

$ ("input"). val ("Hello world!");
--------------------------------------------------------------------------------------------------------------- ---------------

Val (Val)

Check,select,radio and so on can be used to assign value

return value

Jquery

Parameters

Val (array<string>): Value for Check/select

Example

Sets the value of a select and a multiple selected select

HTML Code:

<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><br/>
<input type= "checkbox" value= "Check1"/> Check1
<input type= "checkbox" value= "Check2"/> Check2
<input type= "Radio" value= "Radio1"/> Radio1
<input type= "Radio" value= "Radio2"/> Radio2

JQuery Code:

$ ("#single"). Val ("Single2");
$ ("#multiple"). Val (["Multiple2", "Multiple3"]);
$ ("input"). Val (["Check2", "Radio1"]);

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.