【问题标题】:jquery event being called once for each time the modal is opened每次打开模式时都会调用一次 jquery 事件
【发布时间】:2019-01-06 01:51:35
【问题描述】:

我有一个引导模式表单,其中包含要跟踪的项目列表。如果选中,则有一个全部切换框可以选择/取消选择列表中的所有项目。每次我显示我不理解的模式时,都会调用一次 jquery 函数。 第一次打开modal,jquery函数被调用。 第二次打开modal,jquery函数被调用了两次。 第三次打开modal,jquery函数被调用了3次。 等等。 为什么每次打开模式时都会调用一次此复选框功能? jquery 是否将事物推送到某处的某种类型的堆栈上? 如果用户选择了所有要检查的项目,我想要做的是弹出一个警报。但我不希望他/她收到多个警报。代码如下:

$('#realTimeModal').on('show.bs.modal', function (e) {
  var modal = $(this)
  console.log('controller-1253: realTimeModal handler');

  //first, clear out any previous layers
  clearLayers();
  console.log('show_realtime, updatePositions: ', updatePositions );
  clearInterval(updatePositions);

  /*
   * Check to see if the 'Toggle All' checkbox is checked. If so
   * either select all checkboxes or deselect them.
   */

  $("#check_rt").change(function () {
    console.log('==================================');
    console.log('=====     in #check_rt   =========');
    console.log('==================================');
    var checked = $(this).prop('checked');
    if (checked == true) {
      $.each($("input[class='form-check-input callSign_checkbox-rt']"), function () {
        $(this).prop('checked', true);
      });
      trackAll = 'true';
    } 
    else {
      $.each($("input[class='form-check-input callSign_checkbox-rt']"), function () {
        $(this).prop('checked', false);
      });
      trackAll = 'false';
    }
    //if ( trackAll == 'true' )
      //alert('If you select all aircraft, breadcrumbs will not be displayed.');
  }); 
}) // $('#realTimeModal').on('show.....)

如果用户选中全选复选框,我想要做的只是弹出警报。但我只想要一个警报,而不是打开模式时的警报数量。我在这里缺少 jquery 的基本内容吗?

谢谢.....

【问题讨论】:

    标签: jquery bootstrap-4 event-handling


    【解决方案1】:

    每次打开模式时,您都会添加一个事件处理程序。

    函数-

    $("#check_rt").change(function () { ... }
    

    定义在 -

    $('#realTimeModal').on('show.bs.modal', function (e) { ... }
    

    情况不应该如此。您应该在模态显示函数调用之外调用$("#check_rt").change()

    【讨论】:

    • 我将 $("#check_rt) 移到了模态处理程序之外,现在当我单击/取消单击全部切换复选框时,根本不会调用对全部切换的检查。:(
    • 没有。那是我的错误。谢谢!
    猜你喜欢
    • 2023-03-24
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2023-03-18
    • 2015-10-05
    • 1970-01-01
    相关资源
    最近更新 更多