2017-03-08 70 views
0

我有一個帶刪除按鈕的html表。我想刪除點擊按鈕上的行。爲此,當我點擊一個按鈕類'.btn危險',這將打開一個JQuery警報窗口。我的問題是如何將$(this)按鈕單擊傳遞給$ .alert Yes函數,以便我可以刪除該行。下面我有我的代碼。如何將參數傳遞給jquery警報函數

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.1.0/jquery-confirm.min.css"> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.1.0/jquery-confirm.min.js"></script> 

$(document).ready(function() { 
    $('.btn-danger').on('click', function(e){ 
     e.preventDefault(); 
     var me = $(this); 
     var id = $(this).closest('tr').attr('id'); 
     $.alert({ 
      title: 'Alert!', 
      content: 'Are you sure you want to delete data?', 
      buttons: { 
       Yes: function (me) { 
        //pass value 
       }, 
       No: function() { 
        //close function 
       } 
      } 
     }); 
    }); 
}); 
+0

使VAR我作爲全球 –

回答

2

Yes: function (me) {}刪除me,因爲你已經宣佈它。然後你可以像下面這樣稱呼它。

Yes: function() { 
    console.log(me) 
}, 

$('.btn-danger').on('click', function(e) { 
 
    e.preventDefault(); 
 
    var me = $(this); 
 
    var id = $(this).closest('tr').attr('id'); 
 
    $.alert({ 
 
    title: 'Alert!', 
 
    content: 'Are you sure you want to delete data?', 
 
    buttons: { 
 
     Yes: function() { 
 
     console.log(me) 
 
     //$("tr#" + id).remove() <--- example of removing the row 
 
     }, 
 
     No: function() { 
 
     //close function 
 
     } 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.1.0/jquery-confirm.min.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.1.0/jquery-confirm.min.css" rel="stylesheet" /> 
 

 
<button class="btn btn-danger">delete</button>

+0

謝謝你非常感謝 – nas

+0

@nas你非常歡迎 –

1

你可以嘗試這樣的事情代碼(如果我正確理解你發出)

$(document).ready(function() { 
    $('.btn-danger').on('click', function(e){ 
     e.preventDefault(); 
     var me = $(this); 
     $.alert({ 
      title: 'Alert!', 
      content: 'Are you sure you want to delete data?', 
      buttons: { 
       Yes: function() { 
        me.closest('tr').remove(); 
       }, 
       No: function() { 
        //close function 
       } 
      } 
     }); 
    }); 
}); 

jsfiddle example