2013-11-27 132 views
2

我在html中有此按鈕調用jQuery來確認刪除傳遞價值,jQuery和PHP

<div class='delete' name='<?php echo $story_id; ?>'>delete book</div> 

,這是我的jQuery文件

$(document).ready(function(){ 
    $('.delete').click(function(){ 
     var del_id = $(this).attr('name'); 

     $.confirm({ 
      'type':'POST',     
      'data':'delete_id='+del_id, 
      'title'  : 'Delete Book Confirmation', 
      'message' : 'You are about to delete this book. <br />It cannot be restored at a later time! Do you still want to continue?', 
      'buttons' : { 
       'Yes' : { 
        'class' : 'blue', 
        'action': function(){ 
           document.location.href="sample2.php"; 
        } 

       }, 
       'No' : { 
        'class' : 'gray', 
       } 
      } 
     }); 

    }); 

}); 

的按鈕應該值傳遞給jQuery的並且如果單擊警報中的是按鈕,則該值應該隨後傳遞給sample2.php。我該怎麼辦?對不起,我是新的jquery。 var del_id = $(this).attr('name');不起作用。

+0

div沒有onclick事件或名稱屬性。這是不好的HTML! http://www.w3schools.com/tags/tag_div.asp考慮使用

+0

另外...什麼是'$ .confirm'。那是一個插件嗎? – McGarnagle

回答

2

的$(本).attr( '名稱')工作正常獲取。嘗試提醒以打印該值。

我想你應該從$ .confirm()中刪除數據屬性。像這樣寫。

$(document).ready(function(){ 
     $('.delete').click(function(){ 
     var del_id = $(this).attr('name'); 

     $.confirm({ 
      'type':'POST', 
      'title'  : 'Delete Book Confirmation', 
      'message' : 'You are about to delete this book. <br />It cannot be restored at a later time! Do you still want to continue?', 
      'buttons' : { 
       'Yes' : { 
        'class' : 'blue', 
        'action': function(){ 
           document.location.href="sample2.php?delete_id="+del_id; 
        } 

       }, 
       'No' : { 
        'class' : 'gray', 
       } 
      } 
     }); 

    }); 

});' 

我認爲它會工作。

+0

謝謝@Soumik蘇爾。這對我有用。我剛取代name = <?php echo「$ story_id」; ?> with name ='$ story_id' – Nhiz

1

JQuery.postdata參數應該是一個普通的Javascript對象:

'data': { delete_id: del_id }, 
1

嘗試changediv elementbutton一樣,

<button class='delete' name='<?php echo $story_id; ?>'>delete book</button> 

或代替使用data- attributename attribute一樣,

<div class='delete' data-name='<?php echo $story_id; ?>'>delete book</div> 

它使用data()一樣,

var del_id=$(this).data('name');