我正在处理一个小的jquery脚本,这里是链接http://jsfiddle.net/RbLVQ/60/,我想将所选项目从一个列表框移动到另一个框到其他列表框,但现在我想移动所有项目一次,意味着我会点击它后点击“全选”项目/按钮所有项目必须转到其他列表框可以任何一个帮助吗
的JavaScript
$(document).ready(function() {
$('#btnRight').click(function(e) {
var selectedOpts = $('#lstBox1 option:selected');
if (selectedOpts.length == 0) {
alert("Nothing to move.");
e.preventDefault();
}
$('#lstBox2').append($(selectedOpts).clone());
$(selectedOpts).remove();
e.preventDefault();
});
$('#btnLeft').click(function(e) {
var selectedOpts = $('#lstBox2 option:selected');
if (selectedOpts.length == 0) {
alert("Nothing to move.");
e.preventDefault();
}
$('#lstBox1').append($(selectedOpts).clone());
$(selectedOpts).remove();
e.preventDefault();
});
});
HTML
<table style='width:370px;'>
<tr>
<td style='width:160px;'>
<b>Group 1:</b><br/>
<select multiple="multiple" id='lstBox1'>
<option value="ajax">Ajax</option>
<option value="jquery">jQuery</option>
<option value="javascript">JavaScript</option>
<option value="mootool">MooTools</option>
<option value="prototype">Prototype</option>
<option value="dojo">Dojo</option>
</select>
</td>
<td style='width:50px;text-align:center;vertical-align:middle;'>
<input type='button' id='btnRight' value =' > '/>
<br/><input type='button' id='btnLeft' value =' < '/>
</td>
<td style='width:160px;'>
<b>Group 2: </b><br/>
<select multiple="multiple" id='lstBox2'>
<option value="asp">ASP.NET</option>
<option value="c#">C#</option>
<option value="vb">VB.NET</option>
<option value="java">Java</option>
<option value="php">PHP</option>
<option value="python">Python</option>
</select>
</td>
</tr>
</table>
和一些CSS
body
{
padding:10px;
font-family:verdana;
font-size:8pt;
}
select
{
font-family:verdana;
font-size:8pt;
width : 150px;
height:100px;
}
input
{
text-align: center;
v-align: middle;
}
答案 0 :(得分:4)
你可以这样做
HTML:
<input type='button' id='btnRightAll' value =' >> '/>
JavaScript的:
$('#btnRightAll').click(function(e) {
var selectedOpts = $('#lstBox1 option');
if (selectedOpts.length == 0) {
alert("Nothing to move.");
e.preventDefault();
}
$('#lstBox2').append($(selectedOpts).clone());
$(selectedOpts).remove();
e.preventDefault();
});
从选择器:selected
中删除$('#lstBox1 option')
,然后选择第一个列表框中的所有选项