基於JQUERY的兩個ListBox子項互相調整的實現代碼

來源:互聯網
上載者:User

HTML:
複製代碼 代碼如下:
<div id="divObj3" class="divContent">
<input id="hidColorSelect" name="ColorSelect" type="hidden" value="3,5,6,11,12,13,14" />
<table style="width: 100%; text-align: center;">
<tr class="tr">
<td class="tdr" align="center" width="50%">
<span id="ctl00_ContentPlaceHolder1_WebPanel4_Label7">未選擇顏色</span> :
</td>
<td class="tdl" width="1%">
</td>
<td class="tdr" align="center" width="50%">
<span id="ctl00_ContentPlaceHolder1_WebPanel4_Label8">已選擇顏色</span> :
</td>
</tr>
<tr class="tr">
<td align="right">
<select id="colorUnSelect" multiple="multiple" name="unSelectColors" ondblclick="listMove('colorSelect','colorUnSelect','hidColorSelect',true,this.selectedIndex)" size="8" style="height: 200px; width: 90%;"><option value="4">藍色</option>
<option value="21">紅色132</option>
</select>
</td>
<td>
<img alt="" name="btnRAdd" id="btnRAdd1" src="/Content/images/%E4%B8%8B%E4%B8%80%E9%A1%B5.jpg" style="border-width: 0px;cursor:hand;" onclick="listMove('colorSelect','colorUnSelect','hidColorSelect',true)" />
<img alt="" name="btnRDrop" id="btnRDrop1" src="/Content/images/%E5%89%8D%E4%B8%80%E9%A1%B5.jpg" style="border-width: 0px;cursor:hand;" onclick="listMove('colorSelect','colorUnSelect','hidColorSelect',false)" />
</td>
<td align="left">
<select id="colorSelect" multiple="multiple" name="selectColors"
ondblclick="listMove('colorSelect','colorUnSelect','hidColorSelect',false,this.selectedIndex)"
size="8" style="height: 200px; width: 90%;">
<option value="3">紅色</option>
<option value="5">紫色</option>
<option value="6">黃色</option>
<option value="11">黑色</option>
<option value="12">白色</option>
<option value="13">綠色</option>
<option value="14">粉紅色</option>
</select>
</td>
</tr>
</table>
</div>

對應JS方法:
複製代碼 代碼如下:
function listMove(main, follow, hidetextbox, isBack, index) {
if (index < 0)
return;
var o = undefined;
var source;
var distinct;
var dddd;
if (!isBack) {
//使用getElementById在IE6中存在BUG
source = $('#' + main);// window.document.getElementById(main);
distinct = $('#' + follow); //window.document.getElementById(follow);
}
else {
source = $('#' + follow); // window.document.getElementById(follow);
distinct = $('#' + main); // window.document.getElementById(main);
}
var hid = $('#' + hidetextbox)[0]; // document.getElementById(hidetextbox);
if (index != undefined) {
var op = "option:eq(" + index + ")";
source.find(op).each(function () {
distinct.append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
$(this).remove();
});
}
else {
source.find("option:selected").each(function () {
$(this).remove();
distinct.append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
});
}
var str = "";
//遍曆Listbox,取得選中項的值
$('#' + main + ' option').each(function () {
str += $(this).val() + ',';
});
hid.value = str;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.