Home > Web Front-end > JS Tutorial > body text

Summary of usage using select operation control method

巴扎黑
Release: 2017-06-29 10:12:37
Original
1709 people have browsed it

Many friends are very interested in the operation of jquery, but there are too many things on the Internet, so Script House specially organized the Jquery Select operation method to make it easier for everyone to find.

It should be noted that a lot of the code here is for versions before jquery 1.32 (later versions no longer support @), so just replace it with empty code and test it.

jQuery gets the Text and Value selected by Select:
Syntax explanation:
1. $("#select_id").change(function(){//code. ..}); //Add an event for Select, which is triggered when one of the items is selected
2. var checkText=$("#select_id").find("option:selected").text(); // Get the Text selected by Select
3. var checkValue=$("#select_id").val(); //Get the Value selected by Select
4. var checkIndex=$("#select_id ").get( 0).selectedIndex; //Get the indexvalue of Select
##5. var maxIndex=$("#select_id option:last").attr("index"); // Get the maximum index value of Select


jQuery sets the Text and Value selected by Select:
Syntax explanation:
1. $("#select_id ").get(0) .selectedIndex=1; //Set the item with Select index value to 1 to select
2. $("#select_id ").val(4); //Set the item with Select Value to 4 to select
3 . $("#select_id option[text='jQuery']").attr("selected", true); //Set the Text value of Select to the jQuery item selected



jQuery adds/removes the Option item of Select: Syntax explanation:
1. $("#select_id").append("" ); //Add an Option (drop-down item) to Select
2. $("#select_id").prepend(""); / /Insert an Option for Select (the first position)
3. $("#select_id option:last").remove(); //Delete the Option with the largest index value in Select (the last one)
4. $("#select_id option[index='0']").remove(); //Delete the Option with index value 0 (the first one) in Select
5. $("#select_id option[value= '3']").remove(); //Delete the Option with Value='3' in Select
5. $("#select_id option[text='4']").remove(); // Delete the Option with Text='4' in Select


//Traverse options and add and remove options
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").appendTo($("select[@name=ISHIPTYPE]"));
}
}


//Get the selection value of the drop-down menu

$(#testSelect option:selected').text();
or$("#testSelect").find('option:selected') .text();
or$("#testSelect").val();
/////////////////////////// ///////////////////////////////////////////
If you have a bad memory, you can save it. :
1, drop-down box:

var cc1 = $(".formc select[@name='country'] option[@selected]").text(); //get
The text of the selected item in the drop-down menu (note the space in the middle) var cc2 = $('.formc select[@name="country"]').val(); //Get the selection of the drop-down menu The value of the item
var cc3 = $('.formc select[@name="country"]').attr("id"); //Get the ID attribute value of the selected item in the drop-down menu
$( "#select").empty();//Clear the drop-down box//$("#select").html('');
$(" 2222").appendTo("#sel")//添加下拉框的option
$("#sel").empty();//清空下拉框

获取一组radio被选中项的值
var item = $('input[@name=items][@checked]').val();
获取select被选中项的文本
var item = $("select[@name=items] option[@selected]").text();
select下拉框的第二个元素为当前选中值
$('#select_id')[0].selectedIndex = 1;
radio单选组的第二个元素为当前选中值
$('input[@name=items]').get(1).checked = true;
获取值:
文本框,文本区域:$("#txt").attr("value");
多选框checkbox:$("#checkbox_id").attr("value");
单选组radio: $("input[@type=radio][@checked]").val();
下拉框select: $('#sel').val();
控制表单元素:
文本框,文本区域:$("#txt").attr("value",'');//清空内容
$("#txt").attr("value",'11');//填充内容
多选框checkbox: $("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined) //判断是否已经打勾
单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
$("").appendTo("#sel")//Add the option of the drop-down box
$("#sel").empty();//Clear the drop-down box

For more information, please refer to the following articles:
Jquery Select operation method collection script home special edition

The above is the detailed content of Summary of usage using select operation control method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template