当运行甜蜜警报脚本时如何运行另一个JavaScript函数

时间:2019-01-02 08:35:27

标签: javascript

我要实现javascript功能以删除确认。单击确认警报表ok的{​​{1}}按钮后,应从前端删除。我已将tr中的$("#confirm-btn-alert").click(function()用于确认警报,并将sweet-alert-script.js中的function SomeDeleteRowFunction(o)用于删除newfile.html

sweet-alert-script.js

tr

newfile.html

$(document).ready(function(){
    $("#confirm-btn-alert").click(function(){

        swal({
            title: "Are you sure?",
            text: "Once deleted, you will not be able to recover this imaginary file!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
        .then((willDelete) => {
            if (willDelete) {
                swal("Poof! Your imaginary file has been deleted!", {
                    icon: "success",
                });
            } else {
                swal("Your imaginary file is safe!");
            }
        });

    });

});

2 个答案:

答案 0 :(得分:1)

您可以尝试使用与单击确认按钮相同的方法!
用以下内容更改<script> function

$(document).ready(function(){
    $("#ok-button-id").click(function(o){
        var p=o.parentNode.parentNode;
        p.parentNode.removeChild(p);
    })
});



或者如果<tr>有ID,那么对您来说很容易。
<tr id="myTableRow"><td>Confirmation message here</td></tr>

$(document).ready(function(){
    $("#ok-button-id").click(function(){
         $('#myTableRow').remove();
    })
});


这是另一种方式

 $(document).ready(function(){
    $('#myTableID tr').click(function(){ //Here 'this' will be the table row
        $(this).remove();
        return false;
    })
});

答案 1 :(得分:1)

仅根据信息here在确认table中发现tralert就足够了,如果用户单击确认按钮,则诺言解析为true。如果解除了警报(单击警报的外部),则承诺将解析为null,因此,如果willDeleteif (willDelete)条件下为true,则表示用户确认了该警报,您可以删除自己的内容想要。

我提供了两个示例工作代码,以帮助如下:

$(document).ready(function(){
    function SomeDeleteRowFunction(table,child) { 
        table.find(child).remove();
        // you can also play with table and child (child is tr)
    }
    $(".delete").click(function(){ 
        swal({
            title: "Are you sure?",
            text: "Once deleted, you will not be able to recover this imaginary file!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
        .then((willDelete) => {
            if (willDelete) {
                var $tbl = $(this).closest('table');
                var $tr = $(this).closest('tr'); 
                SomeDeleteRowFunction($tbl,$tr);
                swal("Poof! Your imaginary file has been deleted!", {
                    icon: "success",
                });
            } else {
                swal("Your imaginary file is safe!");
            }
        });

    });
   $("#confirm-btn-alert").click(function(){ 
        swal({
            title: "Are you sure?",
            text: "Once deleted, you will not be able to recover this imaginary file!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
        .then((willDelete) => {
            if (willDelete) {
                var $tbl = $('#tbl2');
                var $tr = $('#tr1'); 
                SomeDeleteRowFunction($tbl,$tr);
                swal("Poof! Your imaginary file has been deleted!", {
                    icon: "success",
                });
            } else {
                swal("Your imaginary file is safe!");
            }
        });

    });
});
<script src="https://sweetalert.js.org/assets/sweetalert/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>first td</td> 
    <td>second td</td>
    <td>TR1 content <button class="delete">Remove</button></td>
  </tr>
  <tr>
    <td>TR2 content<button class="delete">Remove</button></td>
  </tr>
  <tr>
    <td>TR3 content<button class="delete">Remove</button></td>
  </tr>
</table>
<hr/>
<table id="tbl2">
  <tr id="tr1">
    <td>Table 2 TR1 content</td>
  </tr>
  <tr id="tr2">
    <td>Table 2 TR2 content</td>
  </tr>
  <tr id="tr3">
    <td>Table 2 TR3 content</td>
  </tr>
</table>
<button id="confirm-btn-alert">Remove</button>