在MySQL表的上下文中使用jQuery [英] Using jQuery within the context of an MySQL table

查看:88
本文介绍了在MySQL表的上下文中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些jQuery代码难以运行,因为我将其与动态创建的表一起使用(从MySQL表中获取变量).我想在 msg div中显示相应的PHP变量 $ row ['name'] .所以它会说:

I have some jQuery code that is having difficulty running because I'm using it with a dynamically created table (getting variables from a MySQL table). I would like to to display the corresponding PHP variable $row['name'] in the msg div. So it would say something like:

    $("#msg").text($("<?php echo $row['name']; ?> has been booked.");

顺便说一句,我不确定这是否是放置它的正确方法.

Btw I'm not sure if that is the correct way of putting it.

不幸的是,当用户单击 button3 时,它仅更新第一个 msg div,而忽略其余的div.我有什么办法可以动态地将jQuery实例与生成的PHP代码链接起来,以便它可以更新正确的 msg div并在以后显示正确的消息.

Unfortunately when the user clicks on button3 it only updates the first msg div and ignores the rest. Is there any way for me to dynamically link jQuery instances with the PHP code generated so that it can update the correct msg div and display the correct message afterwards.

jQuery代码位于文档的开头,PHP代码位于正文中.

The jQuery code is in the head of my document and the PHP code is in the body.

jQuery代码

<!-- alert -->

<script type="text/javascript">
 $(".button3").click(function() {
    $(this).parent().slideUp("slow", function () {
        $("#msg").text($("button",this).text() + " has been booked.");
        });
    })
</script>

动态创建的表:

if($res->numRows() > 0)
{
    echo "<table class='zebra'>
    <tr>
    <th scope='col'>Name</th>
    <th scope='col'>Capacity</th>
    </tr>";
    while($row = $res->fetchRow())
    {
        echo '<tr align="left">';
        echo "<td>{$row['name']}</td>";
        echo "<td>{$row['capacity']}</td>";

        <div>
        <input type='image' class='button3' src='resources/book.png'
        style='padding-top: 15px;'/>
        </div>

        <div id='msg'>
        </div>
        </td>";     
    }

推荐答案

我没有对其进行测试,但希望对您有所帮助.我要去jsFiddle检查一下.

I didn't test it, but I hope it will help you. I'm going to jsFiddle to check that out.

在这里.我不得不在HTML中进行很多更改,我不确定是否正是您想要的?

Here it is. I had to change a lot of thing in the HTML, I'm not sure if it's exactly what you wanted?

出于懒惰的考虑,我会在包含名称的单元格中添加一个类(这样您就不必担心单元格的顺序了)

For a matter of laziness, I would add a class to the cell containing the name (and this way you won't have to bother about the order of the cells)

    echo "<td class="name">{$row['name']}</td>

然后,您只需将onclick函数替换为:

Then, you can simply replace your onclick function to :

<script type="text/javascript">  $(".button3").click(function() {
    var clickedButton = $(this);
    $(this).parent().slideUp("slow", function () {
        $("#msg").text(clickedButton.parent().parent().find('.name').text() + " has been booked.");
        });
    }) </script>

这篇关于在MySQL表的上下文中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆