与标题相比,我的实际问题相当复杂。我对JavaScript和jquery很新,所以请耐心等待。jquery - 如何从HTML选择中删除选定的选项?
我会申明你在阅读我的问题之前先运行这段代码,这样你才能明白我在做什么。
var selectedAddFootballPlayerId = '';
var selectedAddFootballPlayerName = '';
var selectedRemoveFootballPlayerId = '';
var selectedRemoveFootballPlayerName = '';
$(document).ready(function() {
$('#listboxFootballPlayers option').click(function() {
selectedAddFootballPlayerId = $(this).attr('value');
selectedAddFootballPlayerName = $(this).text();
});
$('#selectedFootballPlayers option').click(function() {
selectedRemoveFootballPlayerId = $(this).attr('value');
selectedRemoveFootballPlayerName = $(this).text();
});
$('input#btnAddFootballPlayerToList').click(function() {
if (selectedAddFootballPlayerId == '') {
alert("Select a football player to be added from the list.");
} else {
var option = new Option(selectedAddFootballPlayerName , selectedAddFootballPlayerId);
$(option).html(selectedAddFootballPlayerName);
$('#selectedFootballPlayers').append(option);
selectedAddFootballPlayerId = '';
selectedAddFootballPlayerName = '';
}
});
$('input#btnRemoveFootballPlayerFromList').click(function() {
if (selectedRemoveFootballPlayerId == '') {
alert("Select a football player to be removed from the list.");
} else {
var option = new Option(selectedRemoveFootballPlayerName , selectedRemoveFootballPlayerId);
$(option).html(selectedRemoveFootballPlayerName);
$('#listboxFootballPlayers').append(option);
selectedRemoveFootballPlayerId = '';
selectedRemoveFootballPlayerName = '';
}
});
});
Cristiano Ronaldo Ricardo Kaka Lionel Messi Gerd Muller Johan Crujjf Franz Beckenbauer David Beckham |
|
之前,我先从问题,请注意:
#listboxFootballPlayers - 列表框左侧
#selectedFootballPlayers - 在右侧
列表框我有2个问题:
1.)在点击->按钮后,如何从#listboxFootballPlayers中删除所选项目/选项。
2)为什么当我在
请问我是否有些问题不清楚。请帮忙。提前致谢。
顺便说一句这里是的jsfiddle链接:
2011-11-19
NinjaBoy
+2
如果您发布工作中的jsfiddle代码或链接到一个网站您将获得更好的答案,让我们不必做尽可能多打字 –
+1
不要试图挑选的运动员存储在每次点击一个选项时都会变量,为什么当单击 - >(或< - )按钮时,您只是从选择框中获取选定的选项?这将使它更简单,可以允许多选,通过键盘而不是鼠标进行选择等。 –
+0
@BrianHoover好吧,我会尝试jsfiddle。这将是我第一次。 –