简体   繁体   English

在 jQuery 中单击两次时删除行正在工作

[英]Delete row is working when clicked twice in jQuery

I have a table with a delete button but the delete button is only working when clicked twice for the first time.我有一个带有删除按钮的表格,但删除按钮仅在第一次单击两次时才起作用。

After deleting one row the button is working perfectly fine ie it is deleting row in one click only.删除一行后,该按钮工作正常,即只需单击即可删除行。

I am not sure why I am seeing this behavior.Any help or suggestion will be appreciated.Thanks.我不确定为什么我会看到这种行为。任何帮助或建议将不胜感激。谢谢。

 function delRow() { $('input[type="button"]').click(function(e) { $(this).closest('tr').remove() }) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table border="0" cellspacing="1" cellpadding="1" id="dataTable" class="graphtable"> <thead> <tr> <td class="headingalign" width="10%">Links</td> <td class="headingalign" width="34%">Desciption</td> <td class="headingalign" width="17%">Image</td> <td class="headingalign" width="17%">URL</td> <td class="headingalign" width="10%"></td> </tr> </thead> <tbody> <tr id="id0" class="vals" name="row"> <xsl:for-each select="//faml/response/quicklinkresponsedto/quicklink/quicklinkdto"> <xsl:variable name="style"> <xsl:choose> <xsl:when test="position() mod 2 = 0">AlterRow2</xsl:when> <xsl:otherwise>AlterRow1</xsl:otherwise> </xsl:choose> </xsl:variable> <tr class='{$style}'> <xsl:variable name="txn_search"> <xsl:value-of select="search" /> </xsl:variable> <xsl:variable name="txn_desc"> <xsl:value-of select="desc" /> </xsl:variable> <xsl:variable name="txn_url"> <xsl:value-of select="url" /> </xsl:variable> <xsl:variable name="txn_img"> <xsl:value-of select="img" /> </xsl:variable> <td> <select type="select-one" id='fldsearch' class="objselect" name="fldsearch"> <option value="S">Select</option> <option value="G">Guides</option> <option value="T">Templates</option> <option value="V">Videos</option> </select> </td> <td><input name="flddesc" value="{$txn_desc}" maxlength="55" size="75" /></td> <td><input name="fldimg" value="{$txn_img}" maxlength="45" size="45" /></td> <td><input name="fldurl" value="{$txn_url}" maxlength="35" size="35" /></td> <td><input tabindex="6" value="Delete Row" class="DeleteButton" type="button" /></td> </tr> </xsl:for-each> </tr> <tr id="id0" class="vals" name="row"> <xsl:for-each select="//faml/response/quicklinkresponsedto/quicklink/quicklinkdto"> <xsl:variable name="style"> <xsl:choose> <xsl:when test="position() mod 2 = 0">AlterRow2</xsl:when> <xsl:otherwise>AlterRow1</xsl:otherwise> </xsl:choose> </xsl:variable> <tr class='{$style}'> <xsl:variable name="txn_search"> <xsl:value-of select="search" /> </xsl:variable> <xsl:variable name="txn_desc"> <xsl:value-of select="desc" /> </xsl:variable> <xsl:variable name="txn_url"> <xsl:value-of select="url" /> </xsl:variable> <xsl:variable name="txn_img"> <xsl:value-of select="img" /> </xsl:variable> <td> <select type="select-one" id='fldsearch' class="objselect" name="fldsearch"> <option value="S">Select</option> <option value="G">Guides</option> <option value="T">Templates</option> <option value="V">Videos</option> </select> </td> <td><input name="flddesc" value="{$txn_desc}" maxlength="55" size="75" /></td> <td><input name="fldimg" value="{$txn_img}" maxlength="45" size="45" /></td> <td><input name="fldurl" value="{$txn_url}" maxlength="35" size="35" /></td> <td><input onClick="return delRow()" tabindex="6" value="Delete Row" class="DeleteButton" type="button" /></td> </tr> </xsl:for-each> </tr> </tbody> </table>

The issue is because you have put a jQuery event handler inside the function, so the first click adds the event handler, and it only runs on the second click.问题是因为您在函数中放置了一个 jQuery 事件处理程序,所以第一次单击会添加事件处理程序,并且它仅在第二次单击时运行。 The additional problem there is that every other click adds another handler.另一个问题是每隔一次点击都会添加另一个处理程序。

You've not shown exactly how you call delRow() , but you can fix the problem by using a delegated event handler:您还没有确切地展示如何调用delRow() ,但您可以通过使用委托事件处理程序来解决该问题:

$('table').on('click', 'input[type="button"]', function() {
  $(this).closest('tr').remove();
})

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

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