溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jQuery選擇性移除列表框的方法

發(fā)布時間:2020-12-04 10:59:58 來源:億速云 閱讀:148 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)jQuery選擇性移除列表框的方法,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

jQuery選擇性移除列表框的方法:綁定向左的方向建按鈕的click事件,當(dāng)單擊按鈕時,右側(cè)列表框選中的項(xiàng)會添加到左側(cè)列表框中,完成移除的操作,代碼為【$(this).remove().appendTo(leftSel)】。

jQuery選擇性移除列表框的方法:

本文將用實(shí)例來講解使用jQuery實(shí)現(xiàn)左右列表框的操作,主要有以下效果:

1、通過左右按鈕向右側(cè)列表框添加項(xiàng)或移除項(xiàng)操作。

2、通過雙擊兩邊列表框里的項(xiàng)可以進(jìn)行添加或移除項(xiàng)。

3、獲取右側(cè)列表框里的選項(xiàng)值。

<div class="select_side"> 
   <p>待選區(qū)</p> 
   <select id="selectL" name="selectL" multiple="multiple"> 
      <option value="13800138000">王新安 - 13800138000</option> 
      <option value="13800138001">李密 - 13800138001</option> 
      <option value="13800138002">姜瑜 - 13800138002</option> 
      <option value="13800138002">錢書記 - 13800138004</option> 
   </select> 
</div> 
<div class="select_opt"> 
   <p id="toright" title="添加">></p> 
   <p id="toleft" title="移除"><</p> 
</div> 
<div class="select_side"> 
   <p>已選區(qū)</p> 
   <select id="selectR" name="selectR" multiple="multiple"> 
   </select> 
</div> 
<div class="sub_btn"><input type="button" id="sub" value="getValue" /></div>

頁面由左右兩個列表框以及操作按鈕項(xiàng)組成。通過CSS來控制三者并排一行。

CSS

.select_side{float:left; width:200px} 
select{width:180px; height:120px} 
.select_opt{float:left; width:40px; height:100%; margin-top:36px} 
.select_opt p{width:26px; height:26px; margin-top:6px; background:url(arr.gif) no-repeat; 
 cursor:pointer; text-indent:-999em} 
.select_opt p#toright{background-position:2px 0} 
.select_opt p#toleft{background-position:2px -22px}

我設(shè)置了兩個列表框都左浮動float:left,同時將操作按鈕項(xiàng)也左浮動,主要就使得三者橫向排列。值得注意是,在設(shè)置操作按鈕時,我使用了一張背景圖片,這張圖片包括了左右兩個方向箭頭的按鈕,如下圖,然后通過background-position來定位圖片的位置,這個方法目前已經(jīng)在很多網(wǎng)站中得到應(yīng)用。

jQuery

首先,綁定向右的方向建按鈕的click事件,當(dāng)單擊按鈕時,左側(cè)列表框選中的項(xiàng)會添加到右側(cè)列表框中,完成添加的操作。

var leftSel = $("#selectL"); 
var rightSel = $("#selectR"); 
$("#toright").bind("click",function(){       
    leftSel.find("option:selected").each(function(){ 
        $(this).remove().appendTo(rightSel); 
    }); 
});

同樣,綁定向左的方向建按鈕的click事件,當(dāng)單擊按鈕時,右側(cè)列表框選中的項(xiàng)會添加到左側(cè)列表框中,完成移除的操作。

$("#toleft").bind("click",function(){        
    rightSel.find("option:selected").each(function(){ 
        $(this).remove().appendTo(leftSel); 
    }); 
});

接下來,需要完成雙擊選擇事件,當(dāng)雙擊該項(xiàng)時,該項(xiàng)立即從該列表框中移除,并添加到與之相對的列表框中。

leftSel.dblclick(function(){ 
    $(this).find("option:selected").each(function(){ 
        $(this).remove().appendTo(rightSel); 
    }); 
}); 
rightSel.dblclick(function(){ 
    $(this).find("option:selected").each(function(){ 
        $(this).remove().appendTo(leftSel); 
    }); 
});

關(guān)于jQuery選擇性移除列表框的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI