JQuery在ajax发布结果时隐藏表行

JQuery Hide table row on ajax post result

本文关键字:隐藏 结果 ajax 布结果 JQuery      更新时间:2023-09-26

我有以下代码。它应该在onClick函数成功时隐藏一行。不幸的是,它并没有隐藏任何东西。我在SO上看到过这个问题,尝试过没有效果的解决方案。

<script type="text/javascript">
    function deleteMsg(row,msgId) {
        //window.alert("deleteMsg(" + msgId + ")");
        $.ajax({
            type: "POST",
            url: "http://localhost:8080/ArcheryScoresV3/profile.jsp",
            data: { cmd:"delete_msg",
                s_token:"dn1ejdmj0dkmgerbm481adkjt0",
                message: msgId },  
            dataType: "json",
            success: function(msg, status, jqXHR){
                if(msg.success == "true") {
                window.alert("successfull, hiding " + row.id);
                //$(this).parents("tr").hide();
                //$(row).hide();
                row.css("background-color", "red");
            } else {
                window.alert("unsuccessfull");
            }
            },            
            error: function(xhr,status,error){
                window.alert("error, status:" + error);
            }
        });
    }
    </script>
     <tr id="row1">
        <td><span onClick="deleteMsg($('row1'),'0ed71375-226e-49ae-aa14-00fbb1f7ed11');" 
                  class="glyphicon glyphicon-trash">
            </span>
        </td>
    </tr>

成功的警报出现了,所以代码正在执行,但我从未看到任何东西消失,甚至变色。所有评论的变体都尝试过,但都没有成功。肯定有一些基本的误解。。

您的选择器错误。onclick = "deleteMsg($('#row1'),)"您忘记了小的"#"

您只需要在Jquery选择器中添加#

<tr id="row1">
    <td>
        <span onClick="deleteMsg($('#row1'),'0ed71375-226e-49ae-aa14-00fbb1f7ed11');" 
            class="glyphicon glyphicon-trash">
        </span>
   </td>
</tr>

然后,您可以在success函数中使用row.hide();来隐藏块(而不是像您尝试的那样使用$(row).hide();