select操作option

jQuery获取Select选择的Text和Value:

代码如下:

$("#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最大的索引值

jQuery添加/删除Select的Option项:

$("#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

内容清空:

$("#charCity").empty();

原文地址:https://www.cnblogs.com/hanxianwu2936/p/11331183.html

时间: 2024-08-30 01:14:16

select操作option的相关文章

js 操作select和option

js 操作select和option 1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select");          mySelect.id = "mySelect";           document.body.appendChild(mySelect);      } 2.添加选项option function addOption(){ //根

有关attribute和property,以及各自对select中option的影响

这个问题老生常谈,但是直到现在我依旧时常会把它搞混.下面列一些各自的特性.   attribute property 设置方法 option.setAttribute('selected', true) option.getAttribute('selected') option.selected = true dom节点表现 会表现在html节点上.打开控制台,可以看到 <option selected=true></option> 不会表现在html中.打开控制台,孤零零的 :

jQuery Select操作大集合

[转载一] 本文总结了jQuery获取Select选择的Text和Value:jQuery添加/删除Select的Option项的Select操作,并给与了对应的解释. jQuery获取Select选择的Text和Value: 语法解释: $("#select_id").change(function(){//code...});    //为Select添加事件,当选择其中一项时触发 var checkText=$("#select_id").find("

AngularJS下拉列表select在option动态变化之后多出了一个错误项的问题

场景: Select初始化之后,选中select的某个选项 通过AngularJS更新select的选项 错误写法: HTML(使用ng-repeat) <div ng-app="TestApp"> <div ng-controller="TestAppCtrl"> <label>options变化之后会出错:</label> <select ng-model="selectedSite"&g

js中select操作实例

window.onload=function(){ //创建select var select1= document.createElement("select"); select1.id="select1"; for(var i=0;i<5;i++){ //创建option var option=document.createElement("option"); //var option=new option(); option.valu

总结jquery中对select和option的基本操作

jQuery获取Select选择的Text和Value:$("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text var checkValue=$("#select_id").val(

select操作大全

每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select> 1.设置value为pxx的项选中 $(".selector").val("pxx"); 2.设置text为pxx的项选中 $(".selector").find("option[text='pxx']").attr(&qu

jQuery对select操作

(转自:http://www.cnblogs.com/as285996985/articles/1535014.html) //遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] option").length if(shipping.value != "CA"){  $("select[@name=ISHIPTYPE]

jQuery对input select操作小结

//遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] option").length if(shipping.value != "CA"){  $("select[@name=ISHIPTYPE] option").each(function(){   if($(this).val() == 111){