首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jquery AJAX问题正在努力尝试,但没有成功

Jquery AJAX问题正在努力尝试,但没有成功
EN

Stack Overflow用户
提问于 2012-08-31 04:27:28
回答 1查看 96关注 0票数 0
代码语言:javascript
运行
复制
<table class="table table-striped">  
            <thead>  
            <tr>  
                <th>Select</th>  
                <th>From</th>  
                <th>Subject</th>  
                <th>Date</th>  
            </tr>
            </thead>
            <tbody>  
        <?php
            //show records
            while ($row = mysql_fetch_array($usrmsg)) {

            $frm_user = $row['from_uid'];
            // Get details from users_profiles table.
            $usrname = mysql_query("select * from `users_profiles` where `id`=$frm_user");
            $usrname = mysql_fetch_array($usrname);
        ?>
            <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td><?php echo $usrname['firstname'] . " " . $usrname['lastname'];?> </td>  
                <td><!-- <a href="<?php echo $row['id']; ?>" id="getmsg"><?php echo substr($row['subject'],0,40); ?>...</a> -->
                 <input class="data" type="text" value="<?php echo $row['id']; ?>" id="<?php print $row['id']; ?>" />
                 <input type="button" href="#get-content" class="clickme" value="<?php echo substr($row['subject'],0,40); ?>.." id="<?php print $row['id']; ?>"/>
                </td>  
                <td><?php echo $row['date']; ?></td>  
            </tr> 
        <?php
            }   
        ?>
        </tbody>  
       </table>  
       <div id="content">
            The table will be placed here
        </div>

我正在从数据库中加载表中的记录。现在,当用户单击主题行时,我的JQUERY AJAX应该会在DIV内容中显示消息。这是正确发生的,但仅针对第一次记录。当我点击第二条记录时,它仍然显示我不想要的第一条记录,我想要的是当用户点击记录时,相应的消息应该显示,而不是总是在第一条。

实际上,我的JQUERY AJAX总是将第一个记录id传递给message.php,在我的例子中是10。

这就是jquery。

代码语言:javascript
运行
复制
<script type="text/javascript" >
    $(document).ready(function(){
        $(".clickme").click(function() {
            $("#content").html('Retrieving...');
            $.ajax({
                type: "GET",
                data: "data=" + $(".data").val(),
                url: "message.php",
                dataType: 'json', 
                success: function(data){
                var msg = data[0];              //get id
                $("#content").html(msg);
                }
            });
        });
    });
</script>

如果有人能帮我那就太好了。

提前THank你..

以下是计划HTML

代码语言:javascript
运行
复制
<table class="table table-striped">  
            <thead>  
            <tr>  
                <th>Select</th>  
                <th>From</th>  
                <th>Subject</th>  
                <th>Date</th>  
            </tr>
            </thead>
            <tbody>  
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="10" id="getmsg">This is proposal for you.....</a> -->
                 <input class="data" type="text" value="10" id="10" />
                 <input type="button" href="#get-content" class="clickme" value="This is proposal for you...." id="10"/>
                </td>  
                <td>2012-08-26</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="11" id="getmsg">All Messages Are Monitored..if we found ...</a> -->
                 <input class="data" type="text" value="11" id="11" />
                 <input type="button" href="#get-content" class="clickme" value="All Messages Are Monitored..if we found .." id="11"/>
                </td>  
                <td>2012-08-26</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="12" id="getmsg">All Messages Are Monitored All Messages ...</a> -->
                 <input class="data" type="text" value="12" id="12" />
                 <input type="button" href="#get-content" class="clickme" value="All Messages Are Monitored All Messages .." id="12"/>
                </td>  
                <td>2012-08-26</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="9" id="getmsg">date...</a> -->
                 <input class="data" type="text" value="9" id="9" />
                 <input type="button" href="#get-content" class="clickme" value="date.." id="9"/>
                </td>  
                <td>2012-08-21</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="1" id="getmsg">...</a> -->
                 <input class="data" type="text" value="1" id="1" />
                 <input type="button" href="#get-content" class="clickme" value=".." id="1"/>
                </td>  
                <td>0000-00-00</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="6" id="getmsg">...</a> -->
                 <input class="data" type="text" value="6" id="6" />
                 <input type="button" href="#get-content" class="clickme" value=".." id="6"/>
                </td>  
                <td>0000-00-00</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="7" id="getmsg">...</a> -->
                 <input class="data" type="text" value="7" id="7" />
                 <input type="button" href="#get-content" class="clickme" value=".." id="7"/>
                </td>  
                <td>0000-00-00</td>  
            </tr> 
                    <tr>  
                <td> <label class="checkbox"><input type="checkbox"></label> </td>  
                <td>Raj Janorkar </td>  
                <td><!-- <a href="8" id="getmsg">Hi want to test you...</a> -->
                 <input class="data" type="text" value="8" id="8" />
                 <input type="button" href="#get-content" class="clickme" value="Hi want to test you.." id="8"/>
                </td>  
                <td>0000-00-00</td>  
            </tr> 
                    </tbody>  
           </table>  
           <div id="content">
                The table will be placed here
            </div>
EN

Stack Overflow用户

回答已采纳

发布于 2012-08-31 04:49:55

正如MrOBRian提到的,你的问题出在$(".data").val()$(".data")将在页面上返回带有data类的所有输入的集合。

您应该将选择器更改为类似于$(this).siblings('.data').val();的内容。这将在单击的按钮旁边为您提供data输入。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12204940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档