duanbicheng3345 2012-03-26 19:49
浏览 16
已采纳

在MySQL表的上下文中使用jQuery

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.

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.

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

jQuery code

<!-- alert -->

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

Dynamically created table:

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>";     
    }
  • 写回答

2条回答 默认 最新

  • douzhang2092 2012-03-26 20:03
    关注

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

    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>
    

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程