2016-03-24 39 views
-1

我有一個問題,我必須在相同的td標籤內顯示/隱藏多個標籤。這裏是我的jQuery代碼:顯示/隱藏JQuery?

$j(document).ready(function() { 
    var records = []; 
    var userID = 'here I am passing user ID' 

    ~[tlist_sql; 
     SELECT ID1, User_ID, ID2 
     From RESERVATIONS 
    ] 
    records.push({'idOne':"~(ID1)",'idTwo':"~(User_ID)",'idThree':"~(ID2)"}); 
    [/tlist_sql] 

    for(var i=0; i< records.length; i++){ 
     //here I am looking for matchin userID in my records array 
     if(records[i].idTwo == userID){ 
     //If match, I want to hide input field and display name. 
     $j('#hide_' + records[i].idOne).parent('.hideEmail').hide().next('.showName').show(); 
     } 
     //Here I'm looking if any of my records idThree has value 0 
     if(records[i].idThree == '0'){ 
     //if match hide input and show word 'Reserved' 
     $j('#hide_' + records[i].idOne).parent('.hideEmail').hide().next('.showResreved').show(); 
     } 
    } 
}); 

這裏是我的html代碼:

<td> 
    <span class="hideEmail"> 
     <input type="text" value="" id="hide_~(ID1)"/> 
    </span> 
    <label class="showName" style="display:none"> 
     <span class="firstLast">Display Name</span> 
    </label> 
    <label class="showReserved" style="display:none"> 
     <span class="notavailable">Reserved</span> 
    </label> 
</td> 

於是問題出現一次,我既相結合的標籤。在第一點,我只有班'showName'標籤和我的邏輯工作正常。但是,一旦我在我的JQuery中添加了一條if語句並添加了另一個類爲'showReserved'的標籤,我的代碼仍然顯示名稱標籤,類'hideEmail'已隱藏,但類'showReserved'從未顯示過。誰能告訴我爲什麼這不起作用?我不應該在jQuery中結合兩個標籤或錯誤嗎?

這是我的工作例如:https://jsfiddle.net/dmilos89/nephebbv/2/

+0

你有一個錯字:'.hide()未來( 'showResreved')'應該是'.hide()未來( 'showReserved')' – forgivenson

+0

不過。不起作用。我的輸入字段已隱藏,但「保留」未顯示。 –

+0

你也不會逃避''',''在這裏我傳遞用戶ID'後沒有分號。你在使用任何類型的IDE嗎? –

回答

0

的問題在這一部分:

$j('#hide_' + records[i].idOne).parent('.hideEmail').hide().next('.showResreved').show(); 

你鍵入 「showReserved」 錯誤。

這裏有一個簡單的解決辦法:

$j('#hide_' + records[i].idOne).parent('.hideEmail').hide().next('.showReserved').show(); 
+0

我修復了這個問題,但仍然無法正常工作。 –