jquery operation select (value, set selected) (turn)

jquery operation select (value, set selected)

jquery operation select (add, delete, empty)  

1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发
 
2. var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的
 
3. var checkValue=$("#select_id").val(); //获取Select选择的Value
 
4. var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值
 
5. var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值
 
jQuery添加/删除Select的Option项:
 
1. $("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)
 
2. $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)
 
3. $("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
 
4. $("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)
 
5. $("#select_id option[value='3']").remove(); //删除Select中Value='3'的Optiona
 
5. $("#select_id option[text='4']").remove(); //删除Select中Text='4'的Optiona
 
内容清空:$("#charCity").empty();

jQuery gets the Text and Value selected by Select: 

$("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发
 
var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的text
 
var checkValue=$("#select_id").val(); //获取Select选择的Value
 
var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值
 
var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值

比如<select class="selector"></select>

1. Set the value to pxx and select

$(".selector").val("pxx");

2. Select the item with text as pxx

$(".selector").find("option[text='pxx']").attr("selected",true);

       There is a usage of square brackets. The equal sign in the square brackets precedes the attribute name without quotation marks. In many cases, the use of brackets can make the logic very simple.

3. Get the value of the currently selected item

$(".selector").val();

4. Get the text of the currently selected item

$(".selector").find("option:selected").text();

    The colon is used here, and mastering its usage will also make the code concise.

 

Many times the cascade of select is used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.

Such as:

$(".selector1").change(function(){
     // 先清空第二个
      $(".selector2").empty();
     // 实际的应用中,这里的option一般都是用循环生成多个了
      var option = $("<option>").val(1).text("pxx");
      $(".selector2").append(option);
});

jQuery add/remove the Option item of Select:
syntax explanation:

$("#select_id").append("<option value='Value'>Text</option>");  //为Select追加一个Option(下拉项)
$("#select_id").prepend("<option value='0'>请选择</option>");  //为Select插入一个Option(第一个位置)
$("#select_id option:last").remove();  //删除Select中索引值最大Option(最后一个)
$("#select_id option[index='0']").remove();  //删除Select中索引值为0的Option(第一个)
$("#select_id option[value='3']").remove();  //删除Select中Value='3'的Option
$("#select_id option[text='4']").remove();  //删除Select中Text='4'的Option

jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related 
1. Get the value of a set of radio selected items 
      var item = $('input[name=items][checked ]').val(); 
2. Get the text of the selected item in select 
      var item = $("select[name=items] option[selected]").text(); 
3. The second element of the select drop-down box Is the currently selected value 
      $('#select_id')[0].selectedIndex = 1; 
4. The second element of the radio single-selection group is the currently selected value 
      $('input[name=items]').get(1) .checked = true; 
5. Get value: 
      text box, text area: $("#txt").attr("value"); 
      multi-select box checkbox: $("#checkbox_id").attr("value") ; 
      Single selection group radio: $("input[type=radio][checked]").val(); 
      drop-down box select: $('#sel').val(); 
6. Control form elements: 
      text box, text area: $("#txt").attr("value",'');//clear content 
      $("#txt").attr("value",'11');//Fill the content 
      checkbox checkbox: $("#chk1").attr("checked",'');//Do not hit hook 
      $ ( "# chk2") attr ( "checked", true);. // tick 
      (. $ ( "# chk1" ) attr ( 'checked') == undefined) if // check determines whether 
      single Select group radio: $("input[type=radio]").attr("checked",'2');//Set the item with value=2 as the currently selected item 
      drop-down box select: $("#sel") .attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item 
      $("<option value='1'>1111</option><option value='2'> 2222</option>").appendTo("#sel")//Add the option of the drop-down box 
      $("#sel").empty();//Empty the drop-down box

 

--THE END--

Guess you like

Origin blog.csdn.net/hn_tzy/article/details/100560813