如何使用jquery选择特定字段

时间:2012-06-20 07:02:16

标签: php jquery

我的代码是

<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
})
})
div字段一次又一次地重复出现在数据库中的字段。所以我想使用jquery获取特定的字段id并使用jquery执行某些操作。 怎么找到这个?

3 个答案:

答案 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(){...