我想在我的項目中添加切換星型功能。點擊此處我將調用此腳本。此代碼無法將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>
刪除類請發表您的HTML部分太 –
如果您'的console.log(StarClass)'你怎麼弄?比較這樣的課是一個壞主意。 – adeneo