简体   繁体   English

js .off()没有工作 <tr> 分子

[英]js .off() is not working on <tr> elements

I have a table with <tr> having ew <input> elements with onchange event declared at design time but filled dynamically from database. 我有一个带有<tr>的表,其中包含在设计时声明但在数据库中动态填充的onchange事件的ew <input>元素。 The HTML of the <td > looks like this:- <td >的HTML如下所示: -

<td scope="col" style="width: 60px; font-weight: normal;">
  <input type="text" id="hall_name" name="hall_name" class="form-control" onchange="rowEdited($(this).parent())" value=<%=hallName %> ></input>
</td>
<td scope="col" style="width: 50px; font-weight: normal; text-align: center">
  <input type="text" style="text-align: center" id="hall_capacity" name="hall_capacity" onchange="rowEdited($(this).parent())" class="form-control" value=<%=hallCapacity %>></input>
</td>
<td scope="col" style="width: 75px; font-weight: normal; text-align: center">
  <input type="text" id="hall_location" name="hall_location" onchange="rowEdited($(this).parent())" class="form-control" value=<%=hallLocation%>></input>
</td>

After cloning the last <tr> , I am attempting to remove the onchange events from all inputs in the last <tr> but it is not working. 在克隆了最后一个<tr> ,我试图从最后一个<tr>中的所有输入中删除onchange事件,但它无法正常工作。

The js code to clone the last <tr> looks like this:- 克隆最后一个<tr>的js代码如下所示: -

var row = $('#hallTable tr:last');
var new_row =  $(row).clone(false,false).off();

The Javascript codes I attempted until now are like this 我到目前为止尝试的Javascript代码是这样的

$('#hallTable tr:last').find("*").off();

and

$('#hallTable tr:last').find(':input').each(function(){
    $(this).unbind('onchange', rowEdited);
})

$('#hallTable tr:last').find('input:checkbox').each(function(){
    $(this).unbind('onchange', rowEdited);

and

    $('#hallTable tr:last').find('input').onchange = null;

and

$('#hallTable tr:last').find('input').unbind('onchange', 'rowEdited');

But none are working. 但没有人工作。 Please advise. 请指教。

The jQuery .off() or unbind functions remove only event listeners that have been added using jQuery. jQuery .off()unbind函数仅删除使用jQuery添加的事件侦听器。 They won't remove inline event listners (like onchange="rowEdited($(this).parent())" ). 他们不会删除内联事件列表器(如onchange="rowEdited($(this).parent())" )。

To remove the inline event listner onchange for all ements that match $('#hallTable tr:last').find('input') you would need to write: 要删除与$('#hallTable tr:last').find('input')匹配的所有元素的内联事件列表器onchange $('#hallTable tr:last').find('input')您需要编写: $('#hallTable tr:last').find('input')

$('#hallTable tr:last').find('input').each(function() {
  this.onchange = null;
})

In general you should not use inline events at all. 通常,您不应该使用内联事件。 Either use addEventListener or if you want to use jQuery then use .on() . 要么使用addEventListener要么使用jQuery,然后使用.on()

Since the event handling code is attached via onclick attributes, you should remove those attributes: 由于事件处理代码是通过onclick属性附加的,因此您应该删除这些属性:

$('#hallTable tr:last input').removeAttr('onclick');

Note that this way you don't have to loop. 请注意,这种方式您不必循环。

This worked for me. 这对我有用。 Please try. 请试试。

$('#hallTable tr:last').find('input').each(function (id, item) {
        item.onchange = null;
});

Should work regardless if the event is added via attributes or programmatically. 无论是通过属性添加事件还是以编程方式添加事件都应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM