简体   繁体   English

单击按钮时删除行

[英]Remove row when button is clicked

I am trying to do this using HtmlService in a Google App Script. 我正在尝试使用Google App脚本中的HtmlService执行此操作。 I have researched it and I cannot figure out why the below doesn't work. 我研究了它,我无法弄清楚为什么以下不起作用。 https://jsfiddle.net/pfue7b71/ https://jsfiddle.net/pfue7b71/

Script 脚本

function removeRow() {
  //  alert("run");
    $(this).closest('tr').remove();
};

Html HTML

<table>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow()" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow()" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow()" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow()" value="X"></td>
    </tr>
</table>

It's because of the context of the function. 这是因为函数的上下文。 The immediate code that runs on the onClick attribute, works using the object context, so it has the right reference to this as the current object, but the call to removeRow is made on Window context, so the reference to this is Window, and not the object. 即在运行的直接代码onClick属性,作品使用对象范围内,因此它具有正确的引用this作为当前对象,但对通话removeRow是在窗口背景下做出的,所以提到this是窗口,而不是物体。 You could solve that with your current code doing this: 您可以使用当前代码解决这个问题:

function removeRow(object){
    $(object).closest('tr').remove();
};

And changing the calls to: 并将通话更改为:

<table>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
</table>

Here you go: https://jsfiddle.net/pfue7b71/2/ 你去吧: https//jsfiddle.net/pfue7b71/2/

Also, for future reference, you should try using console.log instead of alert , and use it to log important things like, for example, $(this) 此外,为了将来参考,您应该尝试使用console.log而不是alert ,并使用它来记录重要的事情,例如$(this)

You need to make sure that this is referencing the DOM element, not the function. 您需要确保this是引用DOM元素,而不是函数。

<table>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
    <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
        <td><input type="button" onClick="removeRow(this)" value="X"></td>
    </tr>
</table>

You also need to rename the function to removeRow , as you're calling it in the HTML (was incorrect in fiddle). 您还需要将该函数重命名为removeRow ,因为您在HTML中调用它(在小提琴中不正确)。

function removeRow(e) {
    $(e).closest('tr').remove();
};

https://jsfiddle.net/pfue7b71/3/ https://jsfiddle.net/pfue7b71/3/

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

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