jQuery動(dòng)態(tài)添加刪除select項(xiàng)(實(shí)現(xiàn)代碼)
// 添加
function col_add() {
var selObj = $("#mySelect");
var value="value";
var text="text";
selObj.append("<option value='"+value+"'>"+text+"</option>");
}
// 刪除
function col_delete() {
var selOpt = $("#mySelect option:selected");
selOpt.remove();
}
// 清空
function col_clear() {
var selOpt = $("#mySelect option");
selOpt.remove();
}
以上方法為jQuery動(dòng)態(tài)添加、刪除和清空select。下面是純js的寫法:
var sid = document.getElementById("mySelect");
sid.options[sid.options.length]=new Option("text","value"); // 在select最后添加一項(xiàng)
其他常用的方法:
$("#mySelect").change(function(){//code...}); //select選中項(xiàng)改變時(shí)觸發(fā)
// 獲取select值
var text=$("#mySelect").find("option:selected").text(); //獲取Select選中項(xiàng)的Text
var value=$("#mySelect").val(); //獲取Select選中項(xiàng)的Value
var value=$("#mySelect option:selected").attr("value"); //獲取Select選中項(xiàng)的Value
var index=$("#mySelect").get(0).selectedIndex; //獲取Select選中項(xiàng)的索引值,從0開始
var index=$("#mySelect option:selected").attr("index"); //不可用?。?!
var index=$("#mySelect option:selected").index(); //獲取Select選中項(xiàng)的索引值,從0開始
var maxIndex=$("#mySelect option:last").attr("index"); //不可用?。。?BR>var maxIndex=$("#mySelect option:last").index();//獲取Select最大索引值,從0開始
$("#mySelect").prepend("<option value='value'>text</option>"); //Select第一項(xiàng)前插入一項(xiàng)
// 設(shè)置select值
//根據(jù)索引設(shè)置選中項(xiàng)
$("#mySelect").get(0).selectedIndex=index;//index為索引值
//根據(jù)value設(shè)置選中項(xiàng)
$("#mySelect").attr("value","newValue");
$("#mySelect").val("newValue");
$("#mySelect").get(0).value = value;
//根據(jù)text設(shè)置對(duì)應(yīng)的項(xiàng)為選中項(xiàng)
var count=$("#mySelect option").length;
for(var i=0;i<count;i++)
{
if($("#mySelect").get(0).options[i].text == text)
{
$("#mySelect").get(0).options[i].selected = true;
break;
}
}
// 清空select
$("#mySelect").empty();
- jquery 如何動(dòng)態(tài)添加、刪除class樣式方法介紹
- jQuery給動(dòng)態(tài)添加的元素綁定事件的方法
- JQuery動(dòng)態(tài)給table添加、刪除行 改進(jìn)版
- 使用jQuery動(dòng)態(tài)加載js腳本文件的方法
- jQuery實(shí)現(xiàn)動(dòng)態(tài)添加和刪除一個(gè)div
- jquery及js實(shí)現(xiàn)動(dòng)態(tài)加載js文件的方法
- jquery動(dòng)態(tài)添加option示例
- jquery getScript動(dòng)態(tài)加載JS方法改進(jìn)詳解
- jQuery動(dòng)態(tài)添加的元素綁定事件處理函數(shù)代碼
- jquery實(shí)現(xiàn)動(dòng)態(tài)添加附件功能
相關(guān)文章
用jquery實(shí)現(xiàn)自定義風(fēng)格的滑動(dòng)條實(shí)現(xiàn)代碼
用jquery實(shí)現(xiàn)自定義風(fēng)格的滑動(dòng)條的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-04-04jquery的trigger和triggerHandler的區(qū)別示例介紹
這篇文章主要介紹了jquery的trigger和triggerHandler的區(qū)別,需要的朋友可以參考下2014-04-04jQuery圖片輪播(二)利用構(gòu)造函數(shù)和原型創(chuàng)建對(duì)象以實(shí)現(xiàn)繼承
本文主要介紹了利用構(gòu)造函數(shù)和原型創(chuàng)建對(duì)象以實(shí)現(xiàn)繼承,并附上完成簡單輪播對(duì)象的封裝的示例代碼。有興趣的朋友可以看下2016-12-12jquery text(),val(),html()方法區(qū)別總結(jié)
jquery text(),val(),html()方法區(qū)別總結(jié)。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11幾行代碼輕松搞定jquery實(shí)現(xiàn)flash8類似的連接效果
幾行代碼輕松搞定jquery實(shí)現(xiàn)flash8類似的連接效果...2007-05-05jQuery編輯器KindEditor4.1.4代碼高亮顯示設(shè)置教程
接下來介紹下編輯器KindEditor4.1.4代碼高亮顯示設(shè)置:加載需要的JS和CSS文件/編輯器初始化設(shè)置后,在里面加prettyPrint等等,感興趣的你可以參考下本文2013-03-03Laravel admin實(shí)現(xiàn)消息提醒、播放音頻功能
這篇文章主要介紹了Laravel admin實(shí)現(xiàn)消息提醒、播放音頻功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07