2012-06-20 95 views
1

我的代碼是如何使用一個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 
}) 
}) 

股利場一次又一次重複的出現在database.So我場想要使用jquery獲取特定的字段id,並使用jquery執行一些操作。 如何找到它?

在這一行

回答

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'); 
      }); 
     }); 

的形式ü可以添加類=「VoteForm」。以便它在父項('')中使用該表單屬性標識。

 $("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'); 
      }); 
     }); 
+0

live ..有什麼用? – StaticVariable

+0

僅供參考請參閱此鏈接:http://api.jquery.com/live/ – Thulasiram

+0

它的作品... thanx的幫助...有沒有其他的方法..excpt parent()..上去或下去.. ???? – StaticVariable

0

$(".vote-up-off).click(function(){... 

它應該是:

$(".vote-up-off").click(function(){... 
1

每個div都需要有自己的id。如果在PHP循環中創建的div,你可以通過增加變量做到這一點:

$divNumber=1; 
while ($row = mysql_fetch_array($results)) 
{ 
    echo '<div id="fields-'.$divNumber.'">'; 
    // echo div content here 
    echo '</div>'; 
    ++$divNumber; 
} 
相關問題