将值传递给jquery和php


Passing value to jquery and php

我在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');不工作。

$(this).attr('name')运行良好。尝试使用警报打印值。

我认为你应该从$.confrm()中删除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',
                }
            }
        });
    });
});'

我认为它会起作用。

JQuery.post的data参数应该是一个普通的Javascript对象:
'data': { delete_id: del_id },

尝试change您的div elementbutton类似

<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>

使用数据()获取,如

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