javascript - Jquery 中多次触发自定义事件

标签 javascript jquery

我到处寻找类似的问题,但是添加 off()、one() 等并不能修复它,这是其他 SO 问题中给出的解决方案。

我有一个表,您可以在其中添加或编辑数据。如果您想编辑数据,表格行数据将加载到选择框中,当您单击“添加”时,它应该删除预览行并添加一个包含修改数据的新行。

问题是在第一次编辑后,由于修改的行是动态添加的,所以它不再起作用。为此,我的解决方案是触发一个自定义事件,我可以在其中使用“委托(delegate)传播”。

到目前为止,我正在尝试触发我的自定义事件,问题是当它被调用时,它会触发 3 到 4 次。我不知道为什么。

这是我的添加按钮代码:

$("#addButton").click(function(e){

      // bunch of code where i get the info from select boxes

      // Here I loop through the table to see if any of the rows ids match current id, 
      // if it does it should delete that row so I call my custom event
      $.each(table, function(index, value) {
         var sid = $(value).children("td:nth-child(1)").data('sitioId');

         // if this matches it means that there already exists a row, so we edit it
         if(sid == sitioId) {
           editRol_id = $(value).children("td:nth-child(4)").children().first().attr('id');
           $(table).trigger('testEvent');
           return false;
      }
});

这是我的自定义事件:

$(table).one('testEvent',function(e, eventInfo) {
  e.preventDefault();
  alert('test');

});

目前它只有一个警报,因为我只是想正确调用它,但警报会触发多次。

如果您需要更多信息或任何内容,请告诉我。感谢您抽出时间。

编辑:这也是我的 html

这是数据所在的表

<div class="panel">
   <div class="col-sm-3"></div>
      <div class="panel-body col-sm-6">
          <div class="table-responsive">
             <table class="table table-hover nomargin" id="relacionRol">
                <thead>
                  <tr>
                    <th>Sitio</th>
                    <th>Rol</th>
                    <th>Categoría</th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
                   <!-- start foreach -->
                   <tr>
                     <td class="sitio_id" data-sitio-id="id" >name</td>
                     <td class="rol_id" data-rol-id="id" >name</td>
                     <td class="categoria_id" data-categoria-id="id1, id2"> name</td>
                     <td>
                        <a href="#" id="editRol<?php echo $i;?>" class="edit"><span class="fa fa-edit"></span></a>
                        <a href="#" id="deleteRol<?php echo $i;?>" class="del"><span class="fa fa-trash"></span></a>
                     </td>
                   </tr>
                   <!-- end foreach -->
                </tbody>
            </table>
        </div><!-- table-responsive -->
    </div>
</div>

这是用于编辑数据的选择框:

<div class="form-group" id="rolSitio1">
    <label class="col-sm-3 control-label">Sitio</label>
    <div class="col-sm-2">
        <select id="sitios1" class="select2 sitio" name="sitio_id" style="width: 100%" data-placeholder="Elegir un sitio">
             <option value="">&nbsp;</option>
             <!-- php for other options -->
         </select>
    </div>
    <label class="col-sm-1 control-label">Rol</label>
    <div class="col-sm-2">
          <select id="roles1" class="select2 rol" name="rol_id" style="width: 100%" data-placeholder="Elegir un rol">
             <option value="">&nbsp;</option>
             <!-- php for other options -->
          </select>
    </div>
    <div id="categoriaWrapper">
       <label class="col-sm-1 control-label">Categoría</label>
       <div class="col-sm-2">
          <select name="categoria_id" style="width: 100%" id="categorias1" class="select2 form-control categoria" multiple>
          <!-- this data is loaded with ajax -->
          </select>
       </div>
    </div>
    <button id="addButton" class="btn"><i class="fa fa-arrow-up"></i></button>
</div>

最佳答案

你似乎在某处有一些table变量。

$.each(table, function(index, value) {

看起来table中有多个项目,因为您对其调用了each。然后:

$(table).one('testEvent'

如果table解析为多个项目/节点,那么每个项目/节点都会收到您的自定义事件,因此您将看到多个警报,就像事件被多次调用一样。

关于javascript - Jquery 中多次触发自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37419526/

相关文章:

javascript - 带有 websockets 的 Express.js

javascript - Datatables.net 选择表上的所有行,包括未显示的行

php - Javascript 在初始页面加载时不加载

javascript - 使用 Div 创建按钮

javascript - 在 NestJS Controller 中检索多个参数 (@Param) 给出未定义的值

javascript - 不使用 ssl 发送应用请求

javascript - 为类设置初始值

javascript - 如何在动态创建的表格中添加样式

jquery contains() 'not' 语法?

javascript - 修复 DRY 代码帮助 javascript?