我到处寻找类似的问题,但是添加 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=""> </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=""> </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/