我的代码是
<div id="fields">
<form id="question-form"> <input type="hidden" name="question-main" value="<?php echo mysql_prep($_GET['ques']);?>" /></form>
<a class="vote-up-off">up vote</a>
<span class="vote-count-post"><?php echo $row["upvotes"]+$row["downvotes"];?></span>
<a class="vote-down-off" id="<?php echo mysql_prep($_GET['ques']);?>" >down vote</a>
<a class="star-off" href="#">favorite</a>
</div>
我的jquery代码是
$("document").ready(function(){
$(".vote-up-off").click(function(){
// here i want the id of form field and votecountpost field which is up and down to this field
})
})
答案 0 :(得分:2)
$("document").ready(function () {
$('.vote-up-off').live('click', function () {
var formId = $(this).parents('form').attr('id');
var voteCountPost = $(this).next('.vote-count-post').attr('id');
});
});
表单你可以添加class =“VoteForm”。这样它就可以在父母('')中使用表单属性id。
$("document").ready(function () {
$('.vote-up-off').live('click', function () {
var formId = $(this).parents('form.VoteForm').attr('id');
var voteCountPost = $(this).next('.vote-count-post').attr('id');
});
});
答案 1 :(得分:1)
每个div都需要自己的id
。如果div是在PHP循环中创建的,则可以通过递增变量来实现:
$divNumber=1;
while ( $row = mysql_fetch_array($results) )
{
echo '<div id="fields-'.$divNumber.'">';
// echo div content here
echo '</div>';
++$divNumber;
}
答案 2 :(得分:0)
在这一行:
$(".vote-up-off).click(function(){...
它应该是:
$(".vote-up-off").click(function(){...