jQuery Get Text and Select selected Value, Value The dynamic attribute value or the like is added

Grammar explanations:
1. $ ( "# select_id") Change (function () {// code ...});.    // add an event to Select, select one of the trigger when
2. var checkText = $ ( " #select_id. ") the Find (" the Option:. ") text (); the selected   // get selected select text
3. CheckValue var = $ (." # select_id ") Val ();   // get selected select Value
4 . var checkIndex = $ ( "# select_id") .get (0) .selectedIndex;   // select Selects an index value obtaining
5. var maxIndex = $ ( "#   select_id option: last") attr ( "index");. // Get the maximum index value select
jQuery selected set select Text and value:
syntax interpretation:
1. $ ( "# select_id") .get (0) .selectedIndex = 1;   // select setting items selected index value of 1
$ 2. ( "# select_id") .val (4);    // set the value select 4 select the value of key
3. $ ( "# select_id option [    text = 'jQuery']").attr("selected", true);   // Set Select Text value of the item is selected jQuery

jQuery add / delete Select the Option key:
grammar explanations:
1. $ ( "# select_id") the append ( "<the Option value = 'Value'> Text </ the Option>");.   // append an Option to Select (dropdown item)
. 2. $ ( "# select_id") The prepend ( "<Option value = '0'> select </ Option>");   // insert a Option (first position) of the select
3. $ ( " Option #select_id: last ") remove ();.   // delete Select the maximum value of the index Option (the last one)
4. $ (" # select_id Option [index = '0'] ") remove ();.   // delete Select the index value Option 0 (first)
5. the $ ( "# select_id Option [value = '. 3']") remove ();.   // delete Select the value = '3' of the Option-
5. the $ ( "Option #select_id [text = '. 4']") remove ();.   // delete the Select text = '4'The Option
 
 

radio jQuery value, checkbox value, SELECT value, select radio, checkbox selected, SELECT is selected, its associated
obtain a set value of the item selected radio
var item = $ ( 'input [ @ name = items] [@ checked .] ') val ();
Get select the selected text item
var item = $ ( "select [@ name = items] Option [@selected]") text ();.
select a second drop-down box to the current element selected value
$ ( '# select_id') [ 0] .selectedIndex = 1;
the second element radio radio group for the currently selected value
. $ ( 'input [@ name = items]') get (1) .checked = true;

Get value:

text box, the text area: $ ( "# txt") attr ( "value");.
checkbox checkbox: $ ( "# checkbox_id" ) attr ( "value");.
radio group radio : $ ( "input [@ type = radio] [@ checked]") val ();.
dropdown select:. $ ( '# sel ') val ();

control form elements:
text boxes, text areas: $ ( "#txt") attr ( "value ", '').;// remove the content of
                 $ ( "# txt") attr ( "value", '11');. // filling contents

Checkbox checkbox: $ ( "# chk1" ) attr ( "checked", ''); // not tick.
                 $ ( "# Chk2") attr ( "the checked", to true); // tick.
                 IF (. $ ( "# chk1" ) attr ( 'checked') == undefined) // check determines whether

the radio group radio: $ ( "input [@ type = radio]") attr ( "checked",. ; '2') // set value = 2 project for the currently selected item
// set value = -sel3 project;) - $ ( "# sel ") attr ( "value", 'sel3': drop-down box select. for the currently selected item
                is added drop-down box $ ( "<option value = ' 1'> 1111 </ option> <option value = '2'> 2222 </ option>"). appendTo ( "# sel") // option
                $ ( "# sel") empty ();. // clear the drop-down box

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

 

//遍历option和添加、移除option
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).remove();
}
});
}else{
$("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));
}
}


// Get the drop-down menu to select a value

$ (# testSelect option: selected ' ) text ();.
or $ ( "# testSelect") find .. (' option: selected ') text ();
or $ ( "# testSelect") Val ();.
/ ////////////////////////////////////////////////// ///////////////
bad memory can collect under:
1, drop-down box:

var cc1 = $ ( "formc select [@ name = 'country'] option [@selected].") text ();. // resulting drop-down menu items of the selected text (note that the intermediate spaces)
var CC2 = $ ( '.formc select [@ name = " country"]') val ();. // value resulting drop-down menu of the selected item
. var cc3 = $ ( '. formc select [@ name = "country"]') attr ( "id"); // get ID attribute value of the selected item pull-down menu
$ ( "# select") empty ();.. // clear the drop-down box // $ ( "# select") html ( '') ;
. $ ( "<option value = '. 1'> 1111 </ option>") the appendTo ( "SELECT #") // Add drop-down box option

Explain a little:
1.select [@ name = 'country'] option [@selected] denotes a name attribute,
and the attribute value 'country' select element having a selected property option elements inside;
can see the @ beginning it means is followed by an attribute.

2, single-box:
$ ( "INPUT [@ type = Radio] [the checked @]") Val (); // get the value of the selected item checkbox (intermediate no space).
$ ( "INPUT [@ type = radio] [@ value = 2] ") attr (." checked ", 'checked'); // set value = checkbox is checked (note 2 with no intervening space).

3, check boxes:
$ ( "INPUT [@ type = CheckBox] [the checked @]") Val (); // get the value of the first box selected item.
$ ( "INPUT [@ type = CheckBox .] [@ checked] ") each (function () {// Since a plurality of check boxes are typically selected, the output can be recycled
Alert ($ (the this) .val ());
});

$ ( "# chk1") attr ( "checked", '');. // uncheck
$ ( "# chk2") attr ( "checked", true);. // check
if ($ ( "# whether chk1 "). attr ( 'checked ') == undefined) {} // check has been determined


Of course, jquery selector is powerful. There are a number of ways.

<script src="jquery-1.2.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#selectTest").change(function()
{
//alert("Hello");
//alert($("#selectTest").attr("name"));
//$("a").attr("href","xx.html");
//window.location.href="xx.html";
//alert($("#selectTest").val());
alert($("#selectTest option[@selected]").text());
$("#selectTest").attr("value", "2");

});
});
</script>


<a href="#">aaass</a>

<! - drop-down box ->
<SELECT ID = "selectTest" name = "selectTest">
<Option value = ". 1">. 11 </ Option>
<Option value = "2"> 22 is </ Option>
<Option value = ". 3"> 33 is </ Option>
<Option value = ". 4"> 44 is </ Option>
<Option value = ". 5"> 55 </ Option>
<Option value = ". 6"> 66 </ Option>
</ sELECT>
jQuery values radio, checkbox value, sELECT value, select radio, checkbox selected, sELECT is selected, and its associated radio obtain a set value of the item selected
var item = $ ( 'input [ @ name = items .] [@ checked] ') val ();
Get select the selected text item
var item = $ ( "select [@ name = items] Option [@selected]") text ();.
select a second drop-down box elements for the currently selected value
$ ( '# select_id') [ 0].selectedIndex = 1;
the second element radio radio group for the currently selected value of
$ ( 'input [@ name = items]') get (1) .checked = true;.
to get the value:
text box, the text area: $ ( "#txt") attr ( "value ").;
Checkbox checkbox: $ ( "# checkbox_id" ) attr ( "value");.
Radio Group radio:. $ ( "Input [ @ type = radio] [@ checked]") val ();
drop-down box select: $ ( '# sel') val ();.
control form elements:
text boxes, text areas:. $ ( "# txt" ) attr ( "value", ''); // Clear contents
$ ( "# txt" ) .attr ( "value", ' 11'); // filled with content
checkbox checkbox:. $ ( "# chk1 ") attr ( "checked", ''); // uncheck
$ ( "# chk2 ") .attr (" checked ", true); // check
. if ($ (" # chk1 ") attr ( 'checked') == undefined) // check determines whether
the radio group radio: $ ( "input [@ type = radio] ") attr ( "checked", '2'); // set value = 2 project for the currently selected item.
drop-down box select:. $ ( "# sel ") attr ( "value ", '- sel3'); // set value = project -sel3 selected item to the current
$ (" <optionvalue = '1 '> 1111 </ option> <optionvalue =' 2 '> 2222 </ option> ") appendTo (." # Sel ") // Add drop-down box Option
$ (" # SEL ") empty ();. / / empty the drop-down box

Obtaining a set value of the item selected radio
var item = $ ( 'input [ @ name = items] [@ checked]') val ();.
Gets select the selected text item
var item = $ ( "select [ @name = items] option [@selected] " ) text ();.
the second element of the drop-down box to select the currently selected value
; $ ( '# select_id') [0] .selectedIndex = 1
second group of radio radio value for the currently selected element
$ ( 'input [@ name = items]') get (1) .checked = true;.
to get the value:
text box, the text area: $ ( "# txt") attr ( "value"). ;
checkbox checkbox: $ ( "# checkbox_id" ) attr ( "value");.
radio group radio:. $ ( "input [ @ type = radio] [@ checked]") val ();
drop-down box select :. $ ( '# sel' ) val ();
control form elements:
text boxes, text areas:. $ ( "# txt" ) attr ( "value", ''); // Clear contents
$ ( "# txt ") .attr (" value ", '11');// fill content
checkbox checkbox: $ ( "# chk1" ) attr ( "checked", ''); // not tick.
$ ( "# Chk2") attr ( "the checked", to true); /. / tick
if (. $ ( "# chk1 ") attr ( 'checked') == undefined) // check determines whether
the radio group radio:. $ ( "input [ @ type = radio]") attr ( "checked" , '2'); // set value = 2 project for the currently selected item
drop-down box select:. $ ( "# sel ") attr ( "value", '- sel3'); // set of value = -sel3 project for the currently selected item
$ ( "<option value = ' 1'> 1111 </ option> <option value = '2'> 2222 </ option>"). appendTo ( "# sel") // add a drop-down box the Option
$ ( "# SEL") empty ();. // clear the drop-down box

 

 

  $ ( "# SelectID option [value = '$ {labcaseSection}']") attr ( "selected", true);. // Select the set value is the last item is selected

Reproduced in: https: //my.oschina.net/usenrong/blog/197896

Guess you like

Origin blog.csdn.net/weixin_33971130/article/details/92028959