当选择特定单选按钮值时,jQuery 显示相对于另一个 div 的 div
因此,我将博客设置为循环从数据库中提取帖子。每个帖子 (#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未经测试,但你可以弄清楚我在这里做了什么:
not tested but you can figure out what I've done here: