bal*_*log 18 javascript asp.net listbox listbox-control
如何使用ASP.NET中的JavaScript将项目从一个列表框控件移动到另一个列表框控件?
Rem*_*arp 32
如果你很高兴使用jQuery,它非常非常简单.
$('#firstSelect option:selected').appendTo('#secondSelect');
Run Code Online (Sandbox Code Playgroud)
其中#firstSelect是选择框的ID.
我在这里列举了一个工作示例:
http://jsbin.com/aluzu(编辑:http://jsbin.com/aluzu/edit)
此代码假定您有一个锚点或在单击时触发移动:
document.getElementById('moveTrigger').onclick = function() {
var listTwo = document.getElementById('secondList');
var options = document.getElementById('firstList').getElementsByTagName('option');
while(options.length != 0) {
listTwo.appendChild(options[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
独立于库的解决方案:
function Move(inputControl)
{
var left = document.getElementById("Left");
var right = document.getElementById("Right");
var from, to;
var bAll = false;
switch (inputControl.value)
{
case '<<':
bAll = true;
// Fall through
case '<':
from = right; to = left;
break;
case '>>':
bAll = true;
// Fall through
case '>':
from = left; to = right;
break;
default:
alert("Check your HTML!");
}
for (var i = from.length - 1; i >= 0; i--)
{
var o = from.options[i];
if (bAll || o.selected)
{
from.remove(i);
try
{
to.add(o, null); // Standard method, fails in IE (6&7 at least)
}
catch (e)
{
to.add(o); // IE only
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<select id="Left" multiple="multiple" size="10">
<option>Some</option>
<option>List</option>
<option>Of</option>
<option>Items</option>
<option>To</option>
<option>Move</option>
<option>Around</option>
</select>
<div id="Toolbar">
<input type="button" value=">" onclick="Move(this)"/>
<input type="button" value=">>" onclick="Move(this)"/>
<input type="button" value="<<" onclick="Move(this)"/>
<input type="button" value="<" onclick="Move(this)"/>
</div>
<select id="Right" multiple="multiple" size="10">
</select>
Run Code Online (Sandbox Code Playgroud)
CSS(示例)
select { width: 200px; float: left; }
#Toolbar { width: 50px; float: left; text-align: center; padding-top: 30px; }
#Toolbar input { width: 40px; }
Run Code Online (Sandbox Code Playgroud)
仅快速测试FF3和IE6&7.
| 归档时间: |
|
| 查看次数: |
18803 次 |
| 最近记录: |