jquery Action Radio box, check box, Drop-down list Implementation code _jquery

Source: Internet
Author: User
1, check box selection operation: In fact, is the use of jquery selector, dot I view the jquery selector
HTML code:
Copy Code code as follows:

<form>
The sports you love are:
<input type= "checkbox" Name= "Item" value= "Football"/> Football
<input type= "checkbox" Name= "Item" value= "basketball"/> Basketball
<input type= "checkbox" Name= "Item" value= "Badminton"/> Badminton
<input type= "checkbox" Name= "Item" value= "Pingpong"/> pingpong
<input type= "button" id= "Checkall" value= "Select All"/>
<input type= "button" id= "Checkfootball" value= "Selected football"/>
</form>

Jquey JS Script:
Copy Code code as follows:

$ (' #checkAll '). Click (Checkall); Select All
$ (' #checkFootball '). Click (Checkfootball); Single-Choice Football
});
function Checkall ()
{
$ (' Input [type= ' checkbox '][name= ' item '] '). attr ("Checked", true);
$ (' [name= ' item ']:checkbox '). attr ("Checked", true);
}

Note: The full counter only needs to replace true here with false.
Select Soccer Action
Jquey JS Script:
Copy Code code as follows:

function Checkfootball ()
{
$ ("[name= ' Item ']:checkbox"). each (function () {
if (this.value = = ' Football ')
{
This.checked = true;
}
})
}

Note: Specific purpose in order to solve from the background to remove data, display. The attr () and Val () methods of jquery are not used to set the value of the current checkbox by using the JavaScript native Dom method, which is more efficient than creating the jquery object
2. Radio button operation
HTML code:
Copy Code code as follows:

A B C D you choose which one:
<input type= "Radio" name= "Item" value= "A"/>a
<input type= "Radio" name= "Item" value= "B"/>b
<input type= "Radio" name= "Item" value= "C"/>c
<input type= "Radio" name= "Item" value= "D"/>d
<input type= "button" id= "Getletter" value= "Get the letter"/>

Initialize selected letter B
Jquey JS Script:
Copy Code code as follows:

$ (document). Ready (function () {
Data initialization selection B.
$ (' [name= ' item ']:radio '). each (function () {
if (this.value = = ' B ')
{
This.checked = true;
}
});
Binding events that get letters
$ (' #getLetter '). Click (Getletter);
});

Get the selected letter
Jquey JS Script:
Copy Code code as follows:

function Getletter ()
{
Alert (' The letter obtained is: ' + $ (' [name= ' item '][checked=true]:radio '). Val ());
}

3. Dropdown box (list) operation
Copy Code code as follows:

<select multiple id= "choose" style= "width=100px;heigth=160px" ></select>
<input id= "addoptions" type= "button" value= "Add Data"/>
<input id= "getselectedoption" type= "button" value= "Get selected value"/>
<input id= "clearoptions" type= "button" value= "Empty list"/>

Jquey JS Script:
Copy Code code as follows:

$ (document). Ready (function () {
$ (' #addOptions '). Click (addoptions); Add an element to a list
$ (' #getSelectedOption '). Click (getselectedoption); Get the selected element
$ (' #clearOptions '). Click (clearoptions); Clear the elements in the list
$ (' #addOptions '). Click (); Trigger adds an element event to the list
});

Append element
Jquey JS Script:
Copy Code code as follows:

function AddOptions ()
{
var Selectcontainer = $ (' #choose ');
for (var i = 0; i < 5; i++)
{
var option = $ (' <option></option> '). Text (' Choose ' + i). val (i);
Selectcontainer.append (option);
}
}

Get the selected element
Copy Code code as follows:

function Getselectedoption ()
{
* * Eject each element individually
var options = $ (' #choose > option:selected ');
$.each (options, function () {
Alert (' option: ' + this.value);
});
/* Eject each element one by one, the first type of shorthand * *
$ (' #choose > option:selected '). each (function () {
Alert (' Option2: ' + this.value);
});
Pop-up data directly, if the Drop-down box, then directly pop-up data, if it is a list and select a number of
According to, the data is separated by ', ' to show
Alert (' val: ' + $ (' #choose '). Val ());
}

Empty list
Copy Code code as follows:

function Clearoptions ()
{
$ (' #choose '). empty ();
}

Commonly used in:
Copy Code code as follows:

var DDL = $ ("#ddlDiaryType option:selected"). Text ()//Drop-down table
var reb = $ ("#RbIfprivate [Checked=true]:radio"). Val ();//Radio Box

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.