如何在编辑字段时将id号发送到Jquery模态表单中


How to send an id number into the Jquery modal form when editing a field?

我是Jquery的新手。我只是想问一下如何将身份证号码发送到这个代码中:dlg.load('view.php?BookID=<?php echo $test['BookID'];?>', function()。当我们想使用Jquery模态表单进行编辑,但它没有得到id号时,这是非常有用的。因此我无法编辑表单。

我的Jquery代码:

$('.edit').click(function(e) {
         e.preventDefault();
         dlg.load('view.php?BookID=<?php echo $test['BookID'];?>', function(){ //i cannot get the BookID number
             dlg.dialog('open');
         });
     });

我的php表代码:

<table border="1">
    <?php
        include("db.php");
    $result=mysql_query("SELECT * FROM books");
    while($test = mysql_fetch_array($result))
    {
        $id = $test['BookID'];
    ?>
        <tr align='center'> 
        <td><font color='black'><?php echo $test['BookID'];?></font></td>
        <td><font color='black'><?php echo $test['Title'];?></font></td>
        <td><font color='black'><?php echo $test['Author'];?></font></td>
        <td><font color='black'><?php echo $test['PublisherName'];?></font></td>
        <td><font color='black'><?php echo $test['CopyrightYear'];?></font></td>    
        <td><a class="edit" href='view.php?BookID=<?php echo $test['BookID'];?>' title="Edit">Edit</a> <!----for editing --->
        <div id="register" ></div>
        <td><a href ='del.php?BookID=<?php echo $test['BookID'];?>' title="Delete"><center>Delete</center></a>  
        </tr>
        <?php
    }
    mysql_close($conn);
    ?>
</table>

您已经在href属性中有了信息,所以您可以使用attr方法从中获取URL:

$('.edit').click(function(e) {
    e.preventDefault();
    dlg.load($(this).attr('href'), function(){
        dlg.dialog('open');
    });
});

它的工作方式:

首先,jQuery将提供上下文,将this对象设置为触发事件的元素,即a元素。

然后,可以通过attr方法检索href属性值。它将返回该HTML属性中的任何值。

由于PHP已经在该属性中注入了URL,因此在单击事件发生时,它很容易获得。

您可以将ID存储在隐藏的输入或数据属性中。让我们使用后者。

<tr align='center' data-book-id="<?php echo $test['BookID'] ?>">

在您的jQuery代码中,获取ID

$('.edit').click(function(e) {
     e.preventDefault();
     // look for the row element that the link belongs to and get the 
     // the value of its data-book-id attribute
     var bookId = $(this).closest('tr').attr('data-book-id');
     // etc
});