将值传递给jquery和php

时间:2013-11-27 04:51:32

标签: php jquery

我在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');无效。

3 个答案:

答案 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.postdata参数应该是普通的Javascript对象:

'data': { delete_id: del_id },

答案 2 :(得分:1)

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

使用data()喜欢

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