jQuery UI Multiselect Next - onChange - 全部删除,全部添加

时间:2012-09-13 11:37:50

标签: javascript jquery jquery-ui multi-select

我有jQuery UI Multiselect Next http://quasipartikel.at/multiselect_next我想在选择以任何方式改变时触发事件。

此示例有效:

<script type="text/javascript">

$(function(){
$("#countries").multiselect();          
$('#countries').multiselect({ selected: function(event, ui) {change(); } });
$('#countries').multiselect({ deselected: function(event, ui) {change(); } });
});         

function change(){
alert('CHANGE');
}

</script>

当我从列表中移动元素时,会调用函数change()。 主要问题是当我按“全部删除”或“全部添加”时,因为该函数被多次调用(元素数量)。

当我按“全部删除”或“全部添加”时,是否可以调用函数change()

感谢。

3 个答案:

答案 0 :(得分:1)

我找到了解决方案:

打开ui.multiselect.js

将以下行添加到selectAll函数:

this.element.trigger('change');

将以下行添加到selectNone函数{

this.element.trigger('change');

添加以下行以停止功能

that.element.trigger('change');

HTML:

<script type="text/javascript">
    $(function(){       
        $("#countries").multiselect();          
        $('#countries').bind('change', function() {alert('Change'); });
        });
</script>

答案 1 :(得分:0)

我不确定。但你试试jquery.one()

$("#foo").one("click", function() {
  alert("This will be displayed only once.");
});

答案 2 :(得分:0)

您可以使用某种计时方案来过滤掉额外的呼叫。让我们假设一个真实的人类不能每100毫秒多次触发所选择或取消选择的事件。您可以执行以下操作:

var lastChange = 0;

function change() {

  //get current date, then the number of milliseconds since 1/1/1970
  var curr_date = new Date();
  var n = cur_date.getTime();

  //compute the difference.
  var diff = n - lastChange;

  //update our last change variable.
  lastChange = n;

  //if we're within 100 milliseconds of the last call, ignore.
  if (diff < 100)
     return;

  //proceed as normal.
  alert('change!');
}

您可以调整这样的算法以满足您的需求,我使用100毫秒作为示例。我不确定是否有更简洁的方法,因为您基本上是在尝试过滤来自UI控件本身的事件。