jQuery addClass在ajax调用后不起作用 [英] jquery addClass not working after ajax call

查看:263
本文介绍了jQuery addClass在ajax调用后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我编写了一个代码,要求在ajax调用之后添加一个类.我确定代码是正确的,但是仍然没有添加该类.它真的很奇怪,因为代码中的其他所有内容都可以正常工作,而且我确定addClass的代码正确,而且我已经检查了控制台是否有任何错误,但是没有错误.这是我的代码

hello i made a code which required to add a class after the ajax call . I am sure the code is right but still the class is not being added . Its really strange as everything else in code works and i am sure the code for addClass is right also i have checked console for any errors but there are no errors . here is my code

$(document).on('click', '.miclickks', function(event) {
 event.preventDefault();

var for_uid    = $(this).parents("li").attr('data');
var for_name   = $(this).parents("li").attr('unme');
var for_pic    = $(this).parents("li").attr('upic');
var owner_uid  = $('.row-fluid').attr('uid');
var owner_name = $('.row-fluid').attr('usnm');
var owner_pic  = $('.row-fluid').attr('usp');
var type       = "kiss";

var dataString = "type=" + type + "&for_uid=" + for_uid + "&for_name=" + for_name + "&for_pic=" + for_pic + "&owner_uid=" + owner_uid + "&owner_pic=" + owner_pic + "&owner_name=" + owner_name; 

        $.ajax({
            type: "POST",
            url: "include/ajax.php",
            data: dataString,
            success: function (html) {
            if(html=="300")
            {
            $('#myModal .modal-body p').html("Error Please Try Again.");

            $('#myModal').modal('show');
            }
            else
            {
            $(this).addClass('active');     


            }
            }
        });



});

推荐答案

两种方式:

第一 :

First:

var that = this;
$.ajax({
    type: "POST",
    url: "include/ajax.php",
    data: dataString,
    success: function (html) {
        if (html == "300") {
            $('#myModal .modal-body p').html("Error Please Try Again.");
            $('#myModal').modal('show');
        } else {
            $(that).addClass('active');
        }
    }
});

第二 (使用context选项):

$.ajax({
    type: "POST",
    url: "include/ajax.php",
    data: dataString,
    context: this,
    success: function (html) {
        if (html == "300") {
            $('#myModal .modal-body p').html("Error Please Try Again.");
            $('#myModal').modal('show');
        } else {
            $(this).addClass('active');
        }
    }
});

这篇关于jQuery addClass在ajax调用后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆