国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery下拉select控件操作方法分享(jquery操作select)

2019-11-20 20:54:42
字體:
來源:轉載
供稿:網友

JQuery獲取和設置Select選項方法匯總如下:

代碼:

復制代碼 代碼如下:

$("#select_id").change(function(){//code...});   //為Select添加事件,當選擇其中一項時觸發(fā)
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_id ").get(0).selectedIndex=1;  //設置Select索引值為1的項選中
$("#select_id ").val(4);   //設置Select的Value值為4的項選中
$("#select_id option[text='jQuery']").attr("selected", true);   //設置Select的Text值為jQuery的項選中

獲取select 選中的 text :

復制代碼 代碼如下:

$("#ddlRegType").find("option:selected").text();

獲取select選中的 value:

復制代碼 代碼如下:

$("#nowamagic").val();

獲取select選中的索引:

復制代碼 代碼如下:

$("#nowamagic").get(0).selectedIndex;

設置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

設置select 選中的索引:

復制代碼 代碼如下:

//index為索引值
$("#nowamagic").get(0).selectedIndex=index;

設置select 選中的value:

復制代碼 代碼如下:

$("#nowamagic").attr("value","Normal");
$("#nowamagic").val("Normal");
$("#nowamagic").get(0).value = value;

設置select 選中的text:

復制代碼 代碼如下:

var count=$("#nowamagicoption").length;
  for(var i=0;i<count;i++) 
     {           if($("#nowamagic").get(0).options[i].text == text) 
        { 
            $("#nowamagic").get(0).options[i].selected = true; 

            break; 
        } 
    }

清空 select:

復制代碼 代碼如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 铜鼓县| 武冈市| 西城区| 政和县| 昌吉市| 米易县| 枝江市| 大理市| 嘉善县| 无棣县| 佛山市| 达日县| 娄烦县| 游戏| 枣庄市| 宜阳县| 舟曲县| 杭锦后旗| 饶河县| 淳安县| 博客| 呼和浩特市| 美姑县| 繁峙县| 合山市| 石狮市| 梓潼县| 梁河县| 东源县| 句容市| 滨海县| 根河市| 鄂尔多斯市| 开远市| 商城县| 石楼县| 垦利县| 含山县| 新宁县| 吴江市| 简阳市|