名为单选按钮的 jQuery 变量无法显示消息

发布于 2024-12-04 14:03:59 字数 1675 浏览 3 评论 0原文

我有一个 DIV #Message_PHPVARABLE,当选择分配给该帖子的单选按钮时,它应该出现在帖子的底部。然而,会发生什么情况,它对于 Post_1 工作正常,但是当我为 Post_2 选择一个选项时,Post_1 的选项发生更改,并且 Message_1 显示新的选择,而不是让 Post_2 选择出现在 Message_2 中...有什么帮助吗?单选按钮提供的选项是“喜欢”和“不喜欢”。

$data = mysql_query("SELECT * FROM Test");
$counter = 1;

while($row = mysql_fetch_array( $data )){
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[name*='like_<?php $counter; ?>']").click(function() {
            var defaultValue = $("label[for*='" + this.id + "']").html();
            var defaultm = "You have chosen : ";
            $('#Message_<?php $counter; ?>').html('').html(defaultm + defaultValue + ' | Value is : ' + $(this).val());
        });

    });
</script>

<div id="post_<?php $counter; ?>" class="post">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
<ul id="listM"></ul>
<div class="left"><p><input id="like_<?php $counter; ?>" type="radio" name="like_<?php $counter; ?>" value="1" />
<label for="like_<?php $counter; ?>">Like</label></p></div>
<div class="right"><p><input id="dislike_<?php $counter; ?>" type="radio" name="like_<?php $counter; ?>" value="0" />
<label for="dislike_<?php $counter; ?>">Dislike</label></p></div>
<hr />
</div>
<div id="Message_<?php $counter; ?>"></div>

<?php 
$counter += 1;
} 
?>

如果我对所有内容进行硬编码(删除 $counter),那么它工作正常,但显然,在 MySQL 中显示无限数量的潜在行,我必须使用变量......

I have a DIV #Message_PHPVARABLE that ought to appear at the bottom of a post when a radio button assigned to this post is selected. What happens however, is that it works fine for Post_1 but when I select an option for Post_2, the option for Post_1 is changed and Message_1 displays the new selection instead of having the Post_2 selection appear in Message_2... any help? The option the radio buttons give is Like and Dislike.

$data = mysql_query("SELECT * FROM Test");
$counter = 1;

while($row = mysql_fetch_array( $data )){
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[name*='like_<?php $counter; ?>']").click(function() {
            var defaultValue = $("label[for*='" + this.id + "']").html();
            var defaultm = "You have chosen : ";
            $('#Message_<?php $counter; ?>').html('').html(defaultm + defaultValue + ' | Value is : ' + $(this).val());
        });

    });
</script>

<div id="post_<?php $counter; ?>" class="post">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
<ul id="listM"></ul>
<div class="left"><p><input id="like_<?php $counter; ?>" type="radio" name="like_<?php $counter; ?>" value="1" />
<label for="like_<?php $counter; ?>">Like</label></p></div>
<div class="right"><p><input id="dislike_<?php $counter; ?>" type="radio" name="like_<?php $counter; ?>" value="0" />
<label for="dislike_<?php $counter; ?>">Dislike</label></p></div>
<hr />
</div>
<div id="Message_<?php $counter; ?>"></div>

<?php 
$counter += 1;
} 
?>

If I hard code everything (removing the $counter) then it works fine but obviously, with an infinite number of potential rows in the MySQL to be displayed, I must use variables...

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

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

发布评论

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

评论(1

静若繁花 2024-12-11 14:03:59

您的 PHP 有问题(除非您错误地输入了您的帖子)

为了打印出 $counter 您需要 echo 它。只需输入 不会将该值放入您的 HTML 中。您需要使用 作为快捷方式。

You have a problem with your PHP (unless you typed your post incorrectly)

In order to print out $counter you need to echo it. Just typing <?php $counter; ?> will not put the value into your HTML. You need to use <?php echo $counter;?> or <?=$counter;?> as a shortcut.

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