如何修复 jQuery 方法,以便可以对多选选项控件中的项目重新排序?
我有一个多选选项选择器和向上/向下按钮:
<select id="animalSelector" multiple="multiple">
<option>Elephant</option>
<option>Duck</option>
<option>Dog</option>
<option>Giraffe</option>
<option>Dinosaur</option>
<option>Whale</option>
</select>
<input type="button" id="btnMoveUp" value="^ Up" />
<input type="button" id="btnMoveDown" value="v Down" />
我希望用户能够突出显示/选择多个选项(使用 CTRL 键),然后能够上下移动他们的选择。
我已经尝试过了:
$('#btnMoveUp').click(function (e) {
moveUp();
e.preventDefault();
});
$('#btnMoveDown').click(function (e) {
moveDown();
e.preventDefault();
});
function moveUp() {
var allOptions = $('#animalSelector').find('option');
allOptions.filter(':selected').each(function () {
var newPosition = allOptions.index(this) - 1;
if (newPosition > -1) {
allOptions.eq(newPosition).before(this);
}
});
}
function moveDown() {
var allOptions = $('#animalSelector').find('option');
var count = allOptions.length;
allOptions.filter(':selected').each(function () {
var newPosition = allOptions.index(this) + 1;
if (newPosition < count) {
allOptions.eq(newPosition).after(this);
}
});
}
然而,这在 IE7 中似乎表现得非常缓慢/古怪,并且当选择开始到达顶部时它会出现一些奇怪的行为。
有人对我处理多选上移/下移行为的最佳方式有任何建议吗?
I have an multiselect option selector and up/down buttons:
<select id="animalSelector" multiple="multiple">
<option>Elephant</option>
<option>Duck</option>
<option>Dog</option>
<option>Giraffe</option>
<option>Dinosaur</option>
<option>Whale</option>
</select>
<input type="button" id="btnMoveUp" value="^ Up" />
<input type="button" id="btnMoveDown" value="v Down" />
I want the user to be able to highlight/select multiple options (using the CTRL key) and then be able to move their selections up and down.
I've taken a stab at it:
$('#btnMoveUp').click(function (e) {
moveUp();
e.preventDefault();
});
$('#btnMoveDown').click(function (e) {
moveDown();
e.preventDefault();
});
function moveUp() {
var allOptions = $('#animalSelector').find('option');
allOptions.filter(':selected').each(function () {
var newPosition = allOptions.index(this) - 1;
if (newPosition > -1) {
allOptions.eq(newPosition).before(this);
}
});
}
function moveDown() {
var allOptions = $('#animalSelector').find('option');
var count = allOptions.length;
allOptions.filter(':selected').each(function () {
var newPosition = allOptions.index(this) + 1;
if (newPosition < count) {
allOptions.eq(newPosition).after(this);
}
});
}
However, this seems to act very slow/quirky in IE7 and it has some weird behavior when the selections start to reach the top.
Does anyone have any suggestions for the best way I can handle multiselect move-up/move-down behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)