Summary of various methods for jQuery to operate input values

Source: Internet
Author: User

Obtain the selected value
Obtains the value of a set of radio selected items.
Copy codeThe Code is as follows:
Var item = $ ('input [@ name = items] [@ checked] '). val ();

Obtain the text of the selected select item.
Copy codeThe Code is as follows:
Var item = $ ("select [@ name = items] option [@ selected]"). text ();

The second element in the select drop-down box is the selected value.
Copy codeThe Code is as follows:
$ ('# Select_id') [0]. selectedIndex = 1;

The second element of the radio Group is the currently selected value.
Copy codeThe Code is as follows:
$ ('Input [@ name = items] '). get (1). checked = true;

Get value:
Text Box, text area:
Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value ");

$ ("# Txt"). val ();

Multiple selection box checkbox:
Copy codeThe Code is as follows:
$ ("# Checkbox_id"). attr ("value ");

Radio Group radio:
Copy codeThe Code is as follows:
$ ("Input [@ type = radio] [@ checked]"). val ();

Drop-down box select:
Copy codeThe Code is as follows:
$ ('# Sel'). val ();

Control form elements:
Text Box, text area:
Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content

Multiple selection box checkbox:
Copy codeThe Code is as follows:
$ ("# Chk1"). attr ("checked", ''); // do not check
$ ("# Chk2"). attr ("checked", true); // check
If ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

Radio Group radio:
Copy codeThe Code is as follows:
$ ("Input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 to the selected one

Drop-down box select:
Copy codeThe Code is as follows:
$ ("# Sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("<Optionvalue = '1'> 1111 </option> <optionvalue = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box
$ ("# Sel"). empty (); // clear the drop-down list

==================================

In Jquery, $ ("# id") is used to obtain the page input element, which is equivalent to document. getElementById ("element"). However, this is a Jquery object instead of a dom element Object. value is the attribute of the dom element Object. therefore, use $ ("# id "). the value cannot be obtained using the following methods: val = $ ("# id") [0]. value; $ ("# id") [0]. value = "new value"; value: $ ("# id") [0]. value = "new value ";
Or $ ("# id"). val ("new value"); val = $ ("# id"). attr ("value ");

========================================

Jquery input text radio check select
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>

<Script src = "jquery-starterkit/lib/jquery-1.3.2.min.js" type = "text/javascript"> </script>

</Head>

<Body>
<Input type = "text" id = "dd" name = "dd" value = "dds"/> dd

<Input name = "rr" id = "rr" type = "radio" value = "34"/> ff
<Input name = "rr" id = "rr2" type = "radio" value = "4"/> 55

<Input name = "ff" type = "checkbox" value = "aa"/> jgdg
<Input name = "ff" type = "checkbox" value = "gd"/> jgdg

<Select name = "ss" id = "ss" size = "1">
<Option value = ""> </option>
<Option value = "8"> d </option>
<Option value = "2"> g </option>
</Select>

<Br/>
<Input type = "button" id = "button" value = "button 1"/>
<Input type = "button" id = "jj" value = "button 2"/>
<Br/>

<Div id = "ssd"> fgoohello </div>

</Body>

<Script language = "javascript" type = "text/javascript">
<! --
$ (Function (){
$ ("# Button"). click (function (){


// Obtain the value
// Alert ($ ('# dd'). val (); // type = text
// Alert ($ ('input [name = rr] [checked] '). val (); // type = radio
// Alert ($ ('input [name = ff] [checked] '). val (); // type = checkbox
// Alert ($ ("select [name = ss] option [selected]"). val (); // select is equal to alert ($ ("# ss option [selected]"). val ());

// Obtain text
// Alert ($ ("select [name = ss] option [selected]"). text (); // select


// Control
/* // Disable # Disable dd
$ ("# Dd"). attr ("disabled", "disabled ");
// Enable # dd unban
$ ("# Jj"). removeAttr ("disabled ");*/


// $ ('Input [name = rr] '). get (0). checked = true; // The first radio is selected
// Alert ($ ("input [type = radio] [value = 34]"). attr ("checked", 'checked'); // radio with value = 34 is selected

// Alert ($ ("input [type = checkbox] [value = gd]"). attr ("checked", 'checked'); // value = gd checkbox is selected
// $ ('Input [name = ff] '). get (1). checked = true; // The first check is selected

 

/* // Select option based on option text
Count = $ ("# ss"). find ("option"). length;
For (var I = 0; I <count; I ++)
{
If ($ ("# ss"). get (0). options [I]. text = 'D ')
{
$ ("# Ss"). get (0). options [I]. selected = true;

Break;
}
}*/

// $ ("<Option value = '1'> 1111 </option> <option value = '25'> 22s22 </option> "). appendTo ("# ss"); // Add option
// $ ("# Ss option [value = 8]"). remove (""); // remove <option value = '8'> d </option>

// $ ("# Ss"). attr ("value", '2'); // select option
// $ ('# Ss') [0]. selectedIndex = 1; // select option

// $ ("# Ss"). empty (); // clear all options

/* // Replace text
Var $ thirdLink = $ ("# ssd ");
Var linkText = $ thirdLink. text (). replace ('foo', 'bar ');
$ ThirdLink. text (linkText );*/

});
});
-->
</Script>
</Html>

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.