当选择特定单选按钮值时,jQuery 显示相对于另一个 div 的 div

发布于 2024-12-10 18:32:05 字数 2625 浏览 0 评论 0原文

因此,我将博客设置为循环从数据库中提取帖子。每个帖子 (#post_) 都与一对单选按钮相连。当选择单选按钮时,ajax 会将值发送到 check.php,并将值放入我的数据库中。然后返回的 html 显示在#Message_ 中。

我想要做到这一点,以便仅当选择 Like 单选按钮时,div (#Message_) 才会出现在该帖子 div 的右上角。

这是我当前拥有的代码,当选择任何单选按钮时显示#Message,但仅在#post 下方显示它。

调用循环:

<div id="content">

<form name="myform" action="" method="post">
        <legend>Posts</legend>

<?php
$data = mysql_query("SELECT * FROM Posts");
while($row = mysql_fetch_array( $data )){
?>

jQuery 本身,位于循环内:

<script type="text/javascript">
$(document).ready(function() {

    $("input[name*='pref_<?php echo $row['postID']; ?>']").click(function() {
        var postID = <?php echo $row['postID']; ?>;
        var value = $(this).val();
        var userID = <?php echo $current_user->ID; ?>;

        $.ajax({
            url: 'check.php',
            type: 'POST',
            data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
            success: function(result) {
                $('#Message_<?php echo $row['postID']; ?>').html('').html(result);
            }
        });

    });

});
</script>

显示循环内的帖子:

<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
    <ul id="listM"></ul>

    <form id="form_<?php echo $row['postID']; ?>" action="/">  
        <fieldset> 
            <div class="left"><p><input id="like_<?php echo $row['postID']; ?>" type="radio" name="pref_<?php echo $row['postID']; ?>" value="1"<?php if ($checked['value'] == "1") echo " checked"; ?> />
            <label for="like_<?php echo $row['postID']; ?>">Like</label></p></div>
            <div class="right"><p><input id="dislike_<?php echo $row['postID']; ?>" type="radio" name="pref_<?php echo $row['postID']; ?>" value="0"<? if ($checked['value'] == "0") echo " checked"; ?> />
            <label for="dislike_<?php echo $row['postID']; ?>">Dislike</label></p></div>
                <hr />
        </fieldset>  
    </form>  
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>"></div>

结束循环:

<?php 
} 
?>

</div>

So I have my blog set up to pull posts from my database in a loop. Each post (#post_) is coupled with a pair of radio buttons. When a radio button is selected, ajax sends the value to check.php where the values are put in my database. The returned html is then displayed in #Message_.

I want to make it so that ONLY when the Like radio button is selected, the div (#Message_) will appear OVER the top right corner of that post's div.

This is the code I currently have which displays the #Message when any radio button is selected but displays it only underneath the #post.

Calling the loop:

<div id="content">

<form name="myform" action="" method="post">
        <legend>Posts</legend>

<?php
$data = mysql_query("SELECT * FROM Posts");
while($row = mysql_fetch_array( $data )){
?>

The jQuery itself, sitting inside the loop:

<script type="text/javascript">
$(document).ready(function() {

    $("input[name*='pref_<?php echo $row['postID']; ?>']").click(function() {
        var postID = <?php echo $row['postID']; ?>;
        var value = $(this).val();
        var userID = <?php echo $current_user->ID; ?>;

        $.ajax({
            url: 'check.php',
            type: 'POST',
            data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
            success: function(result) {
                $('#Message_<?php echo $row['postID']; ?>').html('').html(result);
            }
        });

    });

});
</script>

Display the posts inside the loop:

<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
    <ul id="listM"></ul>

    <form id="form_<?php echo $row['postID']; ?>" action="/">  
        <fieldset> 
            <div class="left"><p><input id="like_<?php echo $row['postID']; ?>" type="radio" name="pref_<?php echo $row['postID']; ?>" value="1"<?php if ($checked['value'] == "1") echo " checked"; ?> />
            <label for="like_<?php echo $row['postID']; ?>">Like</label></p></div>
            <div class="right"><p><input id="dislike_<?php echo $row['postID']; ?>" type="radio" name="pref_<?php echo $row['postID']; ?>" value="0"<? if ($checked['value'] == "0") echo " checked"; ?> />
            <label for="dislike_<?php echo $row['postID']; ?>">Dislike</label></p></div>
                <hr />
        </fieldset>  
    </form>  
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>"></div>

End the loop:

<?php 
} 
?>

</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-12-17 18:32:05

未经测试,但你可以弄清楚我在这里做了什么:

<script type="text/javascript">
$(document).ready(function() {

    $("input[name*='pref_<?php echo $row['postID']; ?>']").click(function() {
        var postID = <?php echo $row['postID']; ?>;
        var value = $(this).val();
        var likeDislike = $(this).id().toString().replace(/(.*)_(.*)/,'$1'); //get 'like or dislike'
        var userID = <?php echo $current_user->ID; ?>;

        $.ajax({
            url: 'check.php',
            type: 'POST',
            data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
            success: function(result) {
                if (likeDislike == 'like') { 
                    //do like stuff
                    //move the message element into the post at the top, use CSS like float:right or text-align:right etc. to position it to the right
                    $('#Message_<?php echo $row['postID']; ?>').prependTo('#'+postID);
                }
                else if (likeDislike == 'dislike') { 
                    //do dislike stuff
                    $('#Message_<?php echo $row['postID']; ?>').html('').html(result);
                }
            }
        });

    });

});
</script>

not tested but you can figure out what I've done here:

<script type="text/javascript">
$(document).ready(function() {

    $("input[name*='pref_<?php echo $row['postID']; ?>']").click(function() {
        var postID = <?php echo $row['postID']; ?>;
        var value = $(this).val();
        var likeDislike = $(this).id().toString().replace(/(.*)_(.*)/,'$1'); //get 'like or dislike'
        var userID = <?php echo $current_user->ID; ?>;

        $.ajax({
            url: 'check.php',
            type: 'POST',
            data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
            success: function(result) {
                if (likeDislike == 'like') { 
                    //do like stuff
                    //move the message element into the post at the top, use CSS like float:right or text-align:right etc. to position it to the right
                    $('#Message_<?php echo $row['postID']; ?>').prependTo('#'+postID);
                }
                else if (likeDislike == 'dislike') { 
                    //do dislike stuff
                    $('#Message_<?php echo $row['postID']; ?>').html('').html(result);
                }
            }
        });

    });

});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文