【问题标题】:Deselect last selected option in multiple select with jQuery使用jQuery取消选择多选中的最后一个选择选项
【发布时间】:2014-09-25 09:04:50
【问题描述】:

好吧,我有一个更改处理程序,可以将值添加到数组中或删除值,以便只在其中包含选定的值。 But when the last element is selected i cannot remove it from the multiple select as the change event does not fire!!有人遇到同样的问题吗?

先谢谢了!

【问题讨论】:

  • 发布您的示例代码。
  • 您应该提供您的代码和html,否则无法帮助您
  • 使用 jsfiddle 以便我们更好地帮助您

标签: javascript jquery html multiple-select


【解决方案1】:

如果你正在做我认为你正在做的事情,那么change 没有理由不被解雇:

<select id="list" multiple="yes">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>


var values = [];
$('#list').change(function(evt) {
    values.push($('#list option:selected').remove().val());
    console.log(values);
});

因此,你不能做我认为你正在做的事情。

除非您选择了最后一个值,否则您无法再次选择它。如果您需要处理对选定选项的点击,请使用点击处理程序,而不是更改处理程序。

【讨论】:

  • 它删除元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
相关资源
最近更新 更多