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

首頁 > 語言 > JavaScript > 正文

JQuery select控件的相關操作實現代碼

2024-05-06 14:21:20
字體:
來源:轉載
供稿:網友
獲取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_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();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宽甸| 吉安县| 浦县| 游戏| 阆中市| 瓮安县| 修武县| 丹东市| 会同县| 商洛市| 和政县| 甘泉县| 德州市| 肥东县| 新巴尔虎右旗| 陆川县| 石家庄市| 贡觉县| 乐平市| 葵青区| 内黄县| 和硕县| 寻乌县| 平凉市| 浦东新区| 陇川县| 云龙县| 五寨县| 平乐县| 应城市| 剑阁县| 渭南市| 九江县| 安吉县| 闸北区| 楚雄市| 汕头市| 黄陵县| 平舆县| 青河县| 册亨县|