简体   繁体   English

使用jQuery更新数据

[英]update data using jquery

I have a page that lists records from a database call. 我有一个页面,列出了来自数据库调用的记录。 I want to have an 'edit' link on each row that will popup a Jquery dialog so that the row can be edited. 我想在每行上都有一个“编辑”链接,这将弹出一个Jquery对话框,以便可以编辑该行。 My question is how do I pass the data from the selected row to the jquery dialog box so that it can be edited? 我的问题是如何将数据从选定的行传递到jquery对话框,以便可以对其进行编辑? i have the code which open jquery popup but the textboxes are empty. 我有打开jquery弹出窗口的代码,但文本框为空。

<div id="boxes">

<div id="dialog2" class="window">
<form method="post" action="update_books_a.php">
  <input name="name" type="text" value="<?php echo $ing['book_name']; ?>"/>
<input name="author" type="text" value="<?php echo $ing['author']; ?>"/> <input type="hidden" name="book_id" value="<?php print $ing['book_id'];?>" />

<input type="submit" value="Update" class="close"/> </form> </div>

myphp code is myphp代码是

$select=mysql_query("select * from books where book_id='$book_id'") or die(mysql_error());
while($ing=mysql_fetch_array($select))
{
 ?>
<tr>
    <td><a href="#dialog2" name="modal"><?php echo $ing['book_name'];?></a></td>
    <td><?php echo $ing['author'];?></td></tr>

the jquery code is 的jQuery代码是




$(document).ready(function() {

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000);

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });

});



body {
font-family:verdana;
font-size:15px;
}

a {color:#333; text-decoration:none}
a:hover {color:#ccc; text-decoration:none}

#mask {
  position:absolute;
  left:0;
  top:0;
  z-index:9000;
  background-color:#000;
  display:none;
}

#boxes .window {
  position:absolute;
  left:0;
  top:0;
  width:440px;
  height:200px;
  display:none;
  z-index:9999;
  padding:20px;
}



#boxes #dialog2 {
  background:url(../images/notice.png) no-repeat 0 0 transparent;
  width:326px;
  height:229px;
  padding:50px 0 20px 25px;
}

You can extract the text value of each element using the .text() method. 您可以使用.text()方法提取每个元素的文本值。
If you need the html value you can use .html() or yourNode.innerHTML 如果需要html值,则可以使用.html()或yourNode.innerHTML

You don't have the book_id in the table, so I can't populate the hidden input for you. 您的表中没有book_id,因此我无法为您填充隐藏的输入。 But the below code shows you how to do it for book_name and author, so hopefully that gives you enough to start with. 但是下面的代码显示了如何为book_name和author进行操作,因此希望可以为您提供足够的起点。

You need to add the code to your click handler, after this line: 您需要在此行之后将代码添加到点击处理程序中:

//Cancel the link behavior
e.preventDefault();

Add this code: 添加此代码:

// Copy book_name
$("#dialog2 input[name=name]").val($(this).text());

// Copy author
$("#dialog2 input[name=author]").val($(this).parent().next("td").text());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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