此代码适用于第一次单击,因为它会更改类以及从 CSS 引用的图像。但是,当我第二次单击时,它的行为就像在我假设已经删除的上一课中单击一样。
if(Model.SeenItWantToSeeIt.Status==1)
{
<div class="movie_data">
<div class="usermovie_option"><a href="javascript:Void(0)" class="dont_want_to_see_it" title="I have seen this movie"> </a></div>
</div>
<div class="clear"></div>
}
else{
<div class="movie_data">
<div class="usermovie_option"><a href="javascript:Void(0)" class="want_to_see_it" title="I have seen this movie"> </a></div>
</div>
<div class="clear"></div>
}
用于切换类的 Javascript 是
$(".want_to_see_it").click(function () {
var wantToSeeIt = $(this);
alert('clicked on want to see it.');
$.ajax({
url: '@Url.Action("SeenIt", "MovieProfile")',
data: { Status: 1, MovieID: movieID },
dataType: 'json',
type: "POST",
success: function (data) {
wantToSeeIt.removeClass();
wantToSeeIt.addClass("dont_want_to_see_it");
$("dont_want_to_see_it").show();
},
error: function (data) {
alert('Error occurred.');
}
});
});
$(".dont_want_to_see_it").click(function () {
alert('clicked on donot want to see it');
var wantToSeeIt = $(this);
$.ajax({
url: '@Url.Action("SeenIt", "MovieProfile")',
data: { Status: 0, MovieID: movieID },
dataType: 'json',
type: "POST",
success: function (data) {
wantToSeeIt.removeClass();
wantToSeeIt.addClass("want_to_see_it");
$("want_to_see_it").show();
},
error: function (data) {
alert('Error occurred.');
}
});
});
问题是每次我点击时它都会显示“点击不想看到它”或“点击想要看到它”作为警报。我所要做的是,每次我点击他们各自的图像时,这条消息都应该交替出现。