JQuery for drop down boxes single box multi box operations

  • 2020-03-30 02:04:25
  • OfStack

The drop-down box:

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

A little explanation:

Select [@ name = 'country'] option [@ selected]

The option element with the selected attribute inside the select element that has the name attribute and the value of that attribute is 'country'. You can see that the one that starts with an @ is followed by a property.

Radio buttons:

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

Check box:

// gets the value of the first item selected in the check box
$(" input [@ type = checkbox] [@ checked] "). The val ();
// because more than one check box is usually selected, the output can be looped
$(" input [@ type = checkbox] [@ checked] "). Each (function () {
Alert ($(this). Val ());
});
/ / no box
$(" # chk1 "). Attr (" checked ", ");
/ / box
$(" # chk2). Attr (" checked ", true);
// check to see if the box has been checked
If ($(" # chk1 "). The attr (" checked ") = = undefined) {}


Related articles: