我在html中有这个按钮,调用jquery来确认删除
<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,如果单击警报中的yes按钮,则该值应随后传递给sample2.php。我该怎么办?对不起,我是jquery的新手。 var del_id = $(this).attr('name');
无效。
答案 0 :(得分:2)
$(this).attr('name')工作正常。尝试提醒以打印值。
我认为你应该从$ .confirm()中删除data属性。并写这样的。
$(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',
}
}
});
});
});'
我认为它会奏效。
答案 1 :(得分:1)
JQuery.post的data
参数应该是普通的Javascript对象:
'data': { delete_id: del_id },
答案 2 :(得分:1)
尝试change
div element
至button
喜欢,
<button class='delete' name='<?php echo $story_id; ?>'>delete book</button>
或使用data- attribute
代替name attribute
喜欢,
<div class='delete' data-name='<?php echo $story_id; ?>'>delete book</div>
使用data()喜欢
获取它var del_id=$(this).data('name');