简体   繁体   中英

Passing value to jquery and php

I have this button in html that calls jquery to confirm a delete

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

and this is my jquery file

$(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',
                }
            }
        });

    });

});

The button should pass the value to jquery and if the yes button in the alert is clicked, the value should subsequently be passed to sample2.php. What should i do? Sorry, i am new with jquery. var del_id = $(this).attr('name'); is not working.

The $(this).attr('name') is working fine. Try an alert to print the value.

I think u should remove the data attribute from the $.confirm(). And write like this.

 $(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',
                }
            }
        });

    });

});'

I think it will work.

JQuery.postdata参数应该是一个普通的Javascript对象:

'data': { delete_id: del_id },

Try to change your div element to button like,

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

Or use data- attribute in place of name attribute like,

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

Fetch it using data() like,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM