如何在jquery中传递数据库id


How to pass database id in jquery

我想从数据库中删除while循环中显示的记录,但在删除之前,我想显示一个确认框。我写的代码如下。它运行良好,但要删除记录,我需要传递我在代码中描述的id

------index.php启动

            <!DOCTYPE html><html><head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>A jQuery Confirm Dialog Replacement with CSS3 | Tutorialzine Demo</title>
        <link href='http://fonts.googleapis.com/css?family=Cuprum&amp;subset=latin' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="css/styles.css" />
        <link rel="stylesheet" type="text/css" href="jquery.confirm/jquery.confirm.css" />
        </head>
        <body>
        <div id="page">
            <?php 
          $sql = "SELECT * FROM tablename";
          $result = db_query($sql);
            while(db_fetch($result))
            {
                ?>
                //here we need to pass the fetched record id to script.js file,but i dont know how
                <div class="item">
                <div class="delete"></div>  //here i have applied css i.e it displays wrong icon, when we click on that icon ,it is showing confirmation box. Everything is perfect in this.. but i wnat to pass and id.. im new to jquery
                </div>
                <?php
            }
            ?>
        </div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script src="jquery.confirm/jquery.confirm.js"></script>
        <script src="js/script.js"></script>
        </body>
        </html>
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>A jQuery Confirm Dialog Replacement with CSS3 | Tutorialzine Demo</title>
        <link href='http://fonts.googleapis.com/css?family=Cuprum&amp;subset=latin' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="css/styles.css" />
        <link rel="stylesheet" type="text/css" href="jquery.confirm/jquery.confirm.css" />
        </head>
        <body>
        <div id="page">
            <?php 
          $sql = "SELECT * FROM tablename";
          $result = db_query($sql);
            while(db_fetch($result))
            {
                ?>
                <div class="item">
                <div class="delete"></div>
                </div>
                <?php
            }
            ?>
        </div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script src="jquery.confirm/jquery.confirm.js"></script>
        <script src="js/script.js"></script>
        </body>
        </html>

----index.php结束
----jquery.confirm.js文件启动

(function($){
    $.confirm = function(params){
        if($('#confirmOverlay').length){
            // A confirm is already shown on the page:
            return false;
        }
        var buttonHTML = '';
        $.each(params.buttons,function(name,obj){
            // Generating the markup for the buttons:
            buttonHTML += '<a href="#" class="button '+obj['class']+'">'+name+'<span></span></a>';
            if(!obj.action){
                obj.action = function(){};
            }
        });
        var markup = [
            '<div id="confirmOverlay">',
            '<div id="confirmBox">',
            '<h1>',params.title,'</h1>',
            '<p>',params.message,'</p>',
            '<div id="confirmButtons">',
            buttonHTML,
            '</div></div></div>'
        ].join('');
        $(markup).hide().appendTo('body').fadeIn();
        var buttons = $('#confirmBox .button'),
            i = 0;
        $.each(params.buttons,function(name,obj){
            buttons.eq(i++).click(function(){
                // Calling the action attribute when a
                // click occurs, and hiding the confirm.
                obj.action();
                $.confirm.hide();
                return false;
            });
        });
    }
    $.confirm.hide = function(){
        $('#confirmOverlay').fadeOut(function(){
            $(this).remove();
        });
    }
})(jQuery);

----jquery.confirm.js文件结束
-----script.js文件启动

$(document).ready(function(){
    $('.item .delete').click(function(){
        var elem = $(this).closest('.item');
        $.confirm({
            'title'     : 'Delete Confirmation',
            'message'   : 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
            'buttons'   : {
                'Yes'   : {
                    'class' : 'blue',
                    'action': function(){
                        elem.slideUp();
                                          //sql delete query will be written here... in where condition i need to pass the fetched record id from index.php file  in where condtion    
                    }
                },
                'No'    : {
                    'class' : 'gray',
                    'action': function(){}  // Nothing to do in this case. You can as well omit the action property.
                }
            }
        });
    });
});

-----script.js结束

您可以使用html数据属性并使用.data jQuery方法检索它

在您的HTML:中

<div class="delete" data-id="{$id}"></div>

在您的Javascript 中

$('.item .delete').click(function(){
  var elem = $(this).closest('.item');
  var id = elem.data('id'); /* this is your db id */
});

您确定要从Javascript发送查询吗?通常的方法是(通过jQuery)向运行查询的脚本(服务器端)发送一个具有特定id的请求,并返回一个响应。

现在,既然您使用while添加了项div,为什么不向div添加一个id属性呢?div包含来自数据库的id,类似

<div id="item<?php echo $row['id'];?>" class="item">
    <div class="delete">
</div>

通过这种方式,$('.item.delete').click处理程序可以通过解析目标的id属性来访问项的id,并且不需要将其显式传递给jQuery。

在这里,您可以使用隐藏字段为id保存值,然后使用jquery从隐藏字段中检索它。。

while(db_fetch($result))
        {
            ?>
            //here we need to pass the fetched record id to script.js file,but i dont know how
            <input type="hidden" id="hid_id" value="<?php echo 'fetched id';?>" />
            <div class="item">
            <div class="delete"></div>  //here i have applied css i.e it displays wrong icon, when we click on that icon ,it is showing confirmation box. Everything is perfect in this.. but i wnat to pass and id.. im new to jquery
            </div>
            <?php
        }
       ?>

然后在jquery中,当使用确认框时,您可以通过id hid_id获得值。