html禁用选定,jquery - 如何从HTML选择中删除选定的选项?

与标题相比,我的实际问题相当复杂。我对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。这将是我第一次。 –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值