JS Jquery for the select dropdownlist checkbox drop down list box in the value of the sample code

  • 2020-03-30 01:20:27
  • OfStack

JQuery gets the Text and Value of the Select:
Grammatical explanation:
1. $(" # select_id "). Change (function () {/ / code... });     // adds an event to the Select, which is triggered when one of the items is selected
2. The var checkText = $(" # select_id "). The find (" option: selected "). The text ();   // gets the Text of the Select
3. The var checkValue = $(" # select_id "). Val ();   // gets the Value of the Select
4. Var checkIndex = $(" # select_id "). The get (0). SelectedIndex;   // gets the index value of the Select selection
5. The var maxIndex = $(" # select_id option: last "). The attr (" index ");   // gets the largest index value   of the Select;

JQuery sets the Text and Value of Select:
Grammatical explanation:
1. $(" # select_id "). The get (0). SelectedIndex = 1;   // sets the selection of an item whose index value is 1
2. $(" # select_id "). Val (4);     // sets the Value of Select to 4
3. $(" # select_id option [text = 'jQuery'] "). The attr (" selected ", true);     // sets the Text value of Select to be selected by jQuery

JQuery add/remove options of Select:
Grammatical explanation:
1. $(" # select_id "). Append (" < The option value = 'value' > Text< / option>" );   // appends an Option to the Select
2. $(" # select_id "). The prepend (" < The option value = '0' > Please select < / option>" );   // insert an Option(first position) for Select
3. $(" # select_id option: last "). The remove ();   // delete the largest Option of the index value in the Select (the last one)
4. $(" # select_id option [index = '0'] "). The remove ();   // delete Option with index value of 0 in Select (the first one)
5. $(" # select_id option (value = '3'] "). The remove ();   // delete the Option Value='3' in Select
5. $(" # select_id option [text = '4'] "). The remove ();   // delete the Option that Text='4' in Select

Jquery radio value, checkbox value, select value, radio select, checkbox select, select, and related & PI;
Gets the value of the selected item of a set of radio  
Var item = $(' input [@ name = items] [@ checked] '). The val ();  
Gets the text   of the selected item of the select;
Var item = $("select[@name=items] option[@selected]").text();  
The second element of the select drop-down box is the currently selected value  
$(' # select_id) [0]. SelectedIndex = 1;  
The second element of the radio radio group is the currently selected value  
$(' input [@ name = items] '). The get (1) checked = true;  

Gets value:  
 
Text field: $("# TXT ").attr("value");  
$("#checkbox_id").attr("value");  
Radio group:     $(" input [@ type = radio] [@ checked] "). The val ();  
Select: $('#sel').val();  

Control form elements:  
Text field: $("# TXT ").attr("value", "); // empty the contents  
                                $(" # TXT "). Attr (" value ", "11"); // fill in the content  

$("#chk1").attr("checked", "); // no check  
                                $(" # chk2). Attr (" checked ", true); / / box  
                                If ($(" # chk1 "). The attr (" checked ") = = undefined) / / determine whether have tick  

Radio group:       $(" input [@ type = radio] "). The attr (" checked ", '2'); // the item with value=2 is the currently selected item  
Drop-down box select:     $(" # sel "). Attr (" value ", '- sel3'); // the item with value=-sel3 is the currently selected item  
                              $(" < The option value = '1' > 1111 < / option> < The option value = '2' > 2222 < / option>" ). AppendTo ("#sel")// add option&appendices to the drop-down box;
                              $(" # sel "). The empty (); // empty the drop-down box

-------------------------------------------------------------------

// traverses options and adds and removes options
The function changeShipMethod (shipping) {
Var len = $("select[@name=ISHIPTYPE] option").length
If (shipping value! = {" CA ")
$(" select [@ name = ISHIPTYPE] option "). Each (function () {
If ($(this). Val () = = 111) {
$(this). The remove ();
}
});
} else {
$(" < The option value = '111' > UPS Ground< / option>" ). AppendTo ($(" select [@ name = ISHIPTYPE] "));
}
}

// gets the value of the dropdown

$(# testSelect option: selected '). The text ();
Or $(" # testSelect "). The find (' option: selected '). The text ();
Or $(" # testSelect "). Val ();
//////////////////////////////////////////////////////////////////

For those with a bad memory:
1. Drop-down box:
 
Var cc1 = $(".formc select[@name='country'] option[@selected]").text(); // gets the text of the selected item in the drop-down menu (note the space in the middle)
Var cc2 = $(' formc select [@ name = "country"] '). The val (); // gets the value of the selected item in the drop-down menu
Var cc3 = $(' formc select [@ name = "country"] '). The attr (" id "); // gets the ID attribute value of the selected item in the drop-down menu
$(" # select "). The empty (); // empty the drop-down box //$("#select").html(");
$(" < The option value = '1' > 1111 < / option>" ).appendto ("#select")// add options to the drop-down box

A little explanation:
Select [@name='country'] option[@selected]
And the option element with the selected attribute inside the select element of 'country';
You can see that the one that starts with an @ is followed by a property.

2. Menu:
$(" input [@ type = radio] [@ checked] "). The val (); // gets the value of the selected item in the marquee (note no space)
$(" input [@ type = radio] [@ value = 2] "). The attr (" checked ", "checked"); // set the menu value=2 as the selected state.

3. Check box:
$(" input [@ type = checkbox] [@ checked] "). The val (); // gets the value of the first item selected in the check box
$("input[@type=checkbox][@checked]"). Each (function()
Alert ($(this). Val ());
});

$(" # chk1 "). Attr (" checked ", "); / / no box
$(" # chk2). Attr (" checked ", true); / / box
If ($(" # chk1 "). The attr (" checked ") = = undefined) {} / / determine whether have tick

Of course jquery's selectors are powerful. There are many other ways.

< Script SRC = "jquery - 1.2.1. Js" type = "text/javascript" > < / script>
< Script language = "javascript" type = "text/javascript" >
The $(document). Ready (function () {
$(" # selectTest "). Change (function ()
{
/ / alert (" Hello ");
/ / alert ($(" # selectTest "). Attr (" name "));
/ / $(" a ".) attr (" href ", "xx. HTML");
/ / the window. The location. Href = "xx. HTML";
/ / alert ($(" # selectTest "). Val ());
Alert ($(" # selectTest option [@ selected] "). The text ());
$(" # selectTest "). Attr (" value ", "2");

});
});
< / script>

< A href = "#" > Aaass< / a>

< ! -- drop-down box -->
< Select id = "selectTest" name = "selectTest >"
< The option value = "1" > 11 < / option>
< The option value = "2" > 22 < / option>
< The option value = "3" > 33 < / option>
< The option value = "4" > 44 < / option>
< The option value = "5" > 55 < / option>
< The option value = "6" > 66 < / option>
< / select>

Jquery radio values, checkbox values, select values, radio selected, checkbox selected, select selected, and related to get the values of a set of selected items of radio
Var item = $(' input [@ name = items] [@ checked] '). The val ();
Gets the text of the selected item in a select
Var item = $("select[@name=items] option[@selected]").text();
The second element of the select drop-down box is the currently selected value
$(' # select_id) [0]. SelectedIndex = 1;
The second element of the radio radio group is the currently selected value
$(' input [@ name = items] '). The get (1) checked = true;

Get the value:
Text field: $("# TXT ").attr("value");
$("#checkbox_id").attr("value");
Radio set radio: $(" input [@ type = radio] [@ checked] "). The val ();
Select: $('#sel').val();

Control form elements:
Text field: $("# TXT ").attr("value", "); // empty the content
$(" # TXT "). Attr (" value ", "11"); // fill in the content
$("#chk1").attr("checked", "); / / no box
$(" # chk2). Attr (" checked ", true); / / box
If ($(" # chk1 "). The attr (" checked ") = = undefined) / / determine whether have tick
Radio set radio: $(" input [@ type = radio] "). The attr (" checked ", '2'); // the item with value=2 is the currently selected item
Select: $("#sel").attr("value",'-sel3'); // the item with value=-sel3 is the currently selected item
$(" < = '1' optionvalueoptionvalue > 1111 < / option> < Optionvalueoptionvalue = '2' > 2222 < / option>" ). AppendTo ("#sel")// add options to the drop-down box
$(" # sel "). The empty (); // empty the drop-down box

Gets the values of a set of selected items of radio
Var item = $(' input [@ name = items] [@ checked] '). The val ();
Gets the text of the selected item in a select
Var item = $("select[@name=items] option[@selected]").text();
The second element of the select drop-down box is the currently selected value
$(' # select_id) [0]. SelectedIndex = 1;
The second element of the radio radio group is the currently selected value
$(' input [@ name = items] '). The get (1) checked = true;

Get the value:
Text field: $("# TXT ").attr("value");
$("#checkbox_id").attr("value");
Radio set radio: $(" input [@ type = radio] [@ checked] "). The val ();
Select: $('#sel').val();

Control form elements:
Text field: $("# TXT ").attr("value", "); // empty the content
$(" # TXT "). Attr (" value ", "11"); // fill in the content
$("#chk1").attr("checked", "); / / no box
$(" # chk2). Attr (" checked ", true); / / box
If ($(" # chk1 "). The attr (" checked ") = = undefined) / / determine whether have tick
Radio set radio: $(" input [@ type = radio] "). The attr (" checked ", '2'); // the item with value=2 is the currently selected item
Select: $("#sel").attr("value",'-sel3'); // the item with value=-sel3 is the currently selected item
$(" < The option value = '1' > 1111 < / option> < The option value = '2' > 2222 < / option>" ). AppendTo ("#sel")// add options to the drop-down box
$(" # sel "). The empty (); // empty the drop-down box


Related articles: