名为单选按钮的 jQuery 变量无法显示消息
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 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 toecho
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.