2017-08-25 144 views
-1

我想在我的項目中添加切換星型功能。點擊此處我將調用此腳本。此代碼無法將starclass的值與字符串中定義的類進行比較。 在這裏,我試圖添加明星/ unstar功能就像Gmail郵件到我的項目。如何將類與js中的字符串進行比較

$(".mailbox-star").click(function (e) { 
    debugger; 
    e.preventDefault(); 

    var $this = $(this).find("a > i"); 
    var glyph = $this.hasClass("glyphicon"); 
    var fa = $this.hasClass("fa"); 
    var msgId = $("#MsgId").val(); 
    var StarClass = $(".mailbox-star i").attr('class'); 
    var StarStatus; 

    if (StarClass === "fa text-yellow fa-star-o") { 
    StarStatus = true; 
    } else { 
    StarStatus = false; 
    } 

    //var starstatus = document.getElementById('ReadstatusStarred'); 
    if (glyph) { 
    $this.toggleClass("glyphicon-star"); 
    $this.toggleClass("glyphicon-star-empty"); 
    } 

    $.ajax({ 
    url: "/Home/Starred", 
    type: "GET", 
    dataType: "json", 
    data: { 
     ChangeStarredStatus: StarStatus, 
     ChangeMessageId: msgId 
    }, 
    success: function (status) { 
     if (status) { 
     alert(status); 
     if (fa) { 
      $this.toggleClass("fa-star"); 
      $this.toggleClass("fa-star-o"); 
     } 
     } 
    }, 
    error: function() { 
     alert("starfailed1"); 
    } 
    }) 
}); 

// HTML代碼從我的控制器使用模型 這裏IM取值。如果我可以在我的js代碼發送IsStarred參數的值,我的問題將被分揀

<table class="table table-hover table-striped"> 
          <tbody> 
           @{int count = 0;} 

           @foreach (var item in Model) 
{ 

    string[] dt = @item.DateTime.ToString().Split(' '); 

    <tr title="@item.DateTime" id="ReadMessage" class="@((item.IsRead != true) ? "row row-highlight" : "row")" > 
     <td><input type="hidden" value="@item.IsRead" id="[email protected]"></td> 
     <td><input type="hidden" value="@item.IsStarred" id="ReadstatusStarred"></td> 
     <td><input type="hidden" id="MsgId" value="@item.MessageId" /></td> 
     <td><input type="checkbox"></td> 
     <td class="mailbox-star" ><a href="#"><i class="@((item.IsStarred==true)? "fa fa-star text-yellow":"fa text-yelow fa-star-o")"></i></a></td> 
     <td class="mailbox-name" id="Text1" onclick="location.href='@Url.Action("Read", "Home", new 
                  { 
                   NameRead = item.FullName, 
                   SubjectRead = item.Subject, 
                   BodyRead = item.Body, 
                   DateRead = item.DateTime, 
                   MessageIdRead= item.MessageId, 

                  })'"> 
      <a href="#" id="Name"> 
       @item.FullName 
      </a> 
     </td> 
     <td class="mailbox-subject" id="Text1"> 

      <b>@item.Subject</b>- 
      @if (item.Body == null || item.Body.Length == 0) 
      { 

      } 
      else 
      { 

       if (item.Body.Length >= 100) 
       { 
        @item.Body.Substring(0, 100) 
       } 
       else 
       { 
        @item.Body 
       } 

      } 
     </td> 

     <td class="mailbox-attachment"></td> 
     <td class="mailbox-date"> 
      @dt[0] 

     </td> 


    </tr> 
      count++; 


} 


          </tbody> 
         </table> 
        </div> 
+0

刪除類請發表您的HTML部分太 –

+1

如果您'的console.log(StarClass)'你怎麼弄?比較這樣的課是一個壞主意。 – adeneo

回答

1

嘗試使用jQuery的is()檢查類而不是

var StarStatus = $(".mailbox-star i").is('.fa, .text-yellow, .fa-star-o') 
+0

謝謝..現在如果條件工作,但阿賈克斯方法的成功屬性沒有反應,你可以檢查 –

+0

所以它提醒錯誤,然後 – adeneo

+0

你可以放棄'.fa'。我知道TS有它,但'.fa- *'圖標總是有'.fa',所以這是一個雙重檢查 – Martijn

0

如果我的描述正確,你想要有類似gmail的東西,點擊來爲一個郵件發送星號,c再舔一下去吧?

很難說什麼是沒有你正在使用的HTML打破,但我會做到這一點通過以下方式:

  1. 當加載從後面的郵件,你必須設置類「starMarked」已加星標郵件取決於您在回來的數據中標記星號郵件的方式,您將檢查某些值是否爲真或等於某個值,然後.addClass(「starMarked」)到該元素。

  2. 綁定。點擊(函數,它下面的邏輯)(你必須在UI什麼列表成員,廣場,圖標,取決於)來表示郵件的所有元素

  3. 的功能該點擊然後檢查該郵件是否被盯住。由於狀態已經由類表示,因此不需要檢查從服務器獲取的數據,也不需要向服務器發送額外的請求以獲取該電子郵件的狀態。這節省了服務器上的時間和負載。 注意:您必須確定請求更改服務器上的狀態,或者您在前端切換和後端狀態的邏輯可能會變得不一致!

  4. 前面的切換可以做很多方法,但最簡單的方法是使用CSS類「starMarked」來表示它已加星標,並且缺少它表示它不是。它給一鳥兩隻鳥(外表和邏輯)。如果您需要檢查狀態,您可以使用.hasClass(「starMarked」)

當切換類使用.removeClass()從元素

相關問題