2011-02-25 152 views
0

我想訪問一個類=「emailSent」的div,但我無法達到div。jquery:訪問元素

這裏是在PHP while循環所產生的HTLML代碼:

<tr><td>TR 1</td></tr><tr> 
    <td colspan="7"> 
     <div class="replyDiv" align="center" style="display:none; margin:10px 60px 10px 10px;"> 

      <form width="350px" id="userReplyForm" name="userReplyForm" method="post" action="#"> 
      <input type="hidden" id="date" name="date" value="'.time().'"/> 
      <input type="hidden" id="sender" name="sender" value="'.$username.'"/> 
      <input type="hidden" id="recipient" name="recipient" value="'.$sender.'"/> 
      <table align="center" width="350px" class="smallTxt userDetails"> 
       <tr> 
        <td align="left" width="350px"><input type="text" id="subject" name="subject" size=30 value="RE:'.$subject.'"/></td></tr> 
       <tr> 
        <td width="350px"><textarea rows="6" cols="42" id="message" name="message"></textarea></td></tr> 
       <tr> 
        <td align="center"><input type="submit" name="Submit" class="submitBtnSmallLong" value="Send Reply"/></td></tr> 
      </table> 
      </form> 

     </div> 
     <div class="emailSent" align="center" style=" margin:10px 60px 10px 10px; color:blue; font-size:18px;"> 
     </div> 
    </td> 
</tr> 

所以我有什麼是對每條記錄生成2行的表中。我如何才能訪問該記錄的相應的div (我不想更新所有div的行,只是一個正在工作): 1)div與類「replyDiv」? 2.)與類「emailSent」的div?

我試圖用$(「。replyDiv」)直接訪問div。但它不起作用。

好,這裏是jQuery的部分:

$(function(){//getUserReply, send to process.php 
     $("form").submit(function(){ 
      var dataString = $(this).serialize(); 
      var message = $(this).find("#message").val(); 

     if(message==""){alert("Please enter your message"); 
      }else{ 
      $.ajax({ 
       type: "POST", 
       url: "process.php", 
       action: "submitForm", 
       data: dataString, 
       dataType: "JSON", 
       cache: false, 
       success: function(data){ 
        if(data == "true"){//hide replyDiv and show emailSent div 
         $("form").closest("tr").find(".emailSent").append("hello"); //this line appends hello to all the .emailSent divs. Remember, I have many rows so only want this particular div being shown.      
         return false; 
        } 

       } 
      }); 

     } 
      return false; 
     }); 
    }); 
+0

好的,所以我發現如果我使用$(「form」)。closest(「tr」)。find(「。emailSent」)。append(「hello」); \t這將訪問所有具有className .emailSent的div。所以現在,我只需要找到一種方法來訪問只有特定的div而不是全部 – Johny 2011-02-25 00:32:10

+0

問題是,從*哪裏*/*何時*你想訪問的元素?你寫*我如何才能訪問該記錄的相應的div *因此,您必須在特定的時刻訪問這些元素。例如。當表單提交時你想訪問元素嗎?或者某個按鈕被按下了? – 2011-02-25 00:33:14

+0

FelixKing,起源點在答覆Div。用戶選擇CORRESPONGING郵件,在第5行說,然後鍵入他們的消息,點擊提交。然後,我將這些數據發送到一個php頁面進行處理,如果一切都恢復正常,在成功之下:function(){//這裏我想隱藏.replyDiv並僅顯示該PARTICULAR當前行的.emailSent div,與一個消息發送成功或某種性質//}我怎麼能達到這個@Johny – Johny 2011-02-25 00:37:06

回答

1

更新:

這裏是你的代碼的版本,應該工作(基本上它只是將我下面寫的,我也改組了一點):

$("form").submit(function(e){ 
    e.preventDefault(); // <- better than `return false` 

    // get reference to elements: 
    var $replyDiv = $(this).parent(); 
    var $emailSent = $(this).parent().next(); 
    // or 
    // var $emailSent = $(this).closest('td').find('.emailSent'); 

    // you should give the form elements classes! 
    if(!$(this).find("#message").val()){ 
     $.ajax({ 
      type: "POST", 
      url: "process.php", // `action` does not exist as option 
      data: $(this).serialize(), 
      dataType: "JSON", 
      cache: false, 
      success: function(data){ 
       if(data == "true"){//hide replyDiv and show emailSent div 
       $replyDiv.hide(); 
       $emailSent.append('hello').show();     
       } 
      } 
     }); 
    } 
    else { 
     alert("Please enter your message"); 
    } 
}); 

根據您最後的評論,我想你將通過Ajax請求將表單數據發送到服務器,並且您想訪問success回調中的某些特定信息。

$('.replyDiv form').submit(function(e) { 
    e.preventDefault(); 

    //    form   emailSent 
    //     v    v 
    var $emailSent = $(this).parent().next(); 
    //      ^
    //      replyDiv 

    $.ajax({ 
     //... 
     success: function() { 
      // here you want the reference 
      $emailSent.text('Worked!'); 
     } 
    }); 
}); 

順便說一句。如果你真的有多個這樣的行,那麼你不能給表單元素ID。

+0

是的,就是這樣的!我會將這部分內容發佈到我原來的帖子中。請看看我的錯誤 – Johny 2011-02-25 00:45:13

+0

哦,我的天!我是一個天才......好吧,你們都是天才,哈哈。非常感謝。它正在工作!謝謝Felix。我真的很喜歡你組織我的代碼人的方式!我希望有一天我的代碼將被高效地編寫成你的代碼 - 現在,我仍然是學習的小菜鳥。 – Johny 2011-02-25 01:06:25

+0

@Johny:你的歡迎:)有趣的是,似乎我總是可以更好地構建其他人的代碼比我自己;)順便說一句。您應該通過點擊旁邊的勾號大綱來接受最能幫助您的答案。你把你的問題標記爲解決了這個問題(你應該爲每個有滿意答案的問題都做這件事,所以你以前的問題也解決了)。 – 2011-02-25 01:12:31

2

看看你的代碼,你將它定義爲一個類不能作爲ID

<div class="emailSent" align="center" style=" margin:10px 60px 10px 10px; color:blue; font-size:18px;"> 
    </div> 

需要改變類ID

<div id="emailSent" align="center" style=" margin:10px 60px 10px 10px; color:blue; font-size:18px;"> 
    </div> 
+0

但如果我將其更改爲一個ID,並且由於我將有多個具有相同ID的行,那麼我將如何訪問,比如說第4行中的div? – Johny 2011-02-25 00:23:10

+0

反正你不能多次使用一個ID。 jQuery將始終選擇使用該名稱查找的第一個ID。 – iwasrobbed 2011-02-25 00:25:48

+0

這正是我的觀點。我在原始帖子中輸入了錯字。我有我的div設置爲.className。我的問題是,如果我試過的東西不能讓我訪問這些div,我該如何訪問這些div? – Johny 2011-02-25 00:29:01