最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - changing the class of clicked button on ajax function success - Stack Overflow

programmeradmin2浏览0评论

i have a several html buttons (not within a form).

When clicked they call a JavaScript function that after conformation, runs an ajax call. This part all works OK, but i would then like to change the class of whichever button was clicked on success of the ajax call.

i have tried various methods seen on stackOverflow, but none of them seem to work... can i please ask what am i doing wrong?

here is the simplified HTML (buttons)

<button class="btn btn-primary" onclick="primaryImage(4107,19372,'/abbie1.jpg'); return false;">
set as profile image
</button>

<button class="btn btn-primary" onclick="primaryImage(4107,19373,'/abbie2.jpg'); return false;">
set as profile image
</button>

<button class="btn btn-success" onclick="primaryImage(4107,19374,'/abbie3.jpg'); return false;" disabled="disabled">
profile image
</button>

Please note: the last button is already the active/success button, and i would also like to remove the class on success too (as only one should be active), but that is maybe my next stage....

here is the javaScript, (i have left in some of the methods i have tried, but mented them out)

function primaryImage(eid,pid)
    {
        if (confirm("Are you sure you wish to use this as your profile image?"))
        {

            $.ajax({
                type: "POST",
                async: false,
                cache: false,
                dataType: "json",
                url: "ajax_photo.php",
                data: "action=primary&eid="+eid+"&pid="+pid,
                //context: this,
                success: function(data){
                    if(data.result=='success')
                    {
                        alert('The image is now set as the profile image');
                        //$('button').click(function(){
                        //   $(this).addClass('btn-success');
                        //});

                        //$('button').on(data.result=='success', function(e) {
                        //  $(this).toggleClass("btn btn-success"); //you can list several class names 
                        //  e.preventDefault();
                        //});

                        //$(this).removeClass('btn-primary').addClass('btn-success');

                    }
                    else
                    {
                        alert('An error occurred when trying to set the image: ' + data.result);
                    }
               }
            });
        }
    }

I would be very grateful for any advice of what i am doing wrong (as you can see, i am not too good with JS (yet))

Thanks! Ford

i have a several html buttons (not within a form).

When clicked they call a JavaScript function that after conformation, runs an ajax call. This part all works OK, but i would then like to change the class of whichever button was clicked on success of the ajax call.

i have tried various methods seen on stackOverflow, but none of them seem to work... can i please ask what am i doing wrong?

here is the simplified HTML (buttons)

<button class="btn btn-primary" onclick="primaryImage(4107,19372,'/abbie1.jpg'); return false;">
set as profile image
</button>

<button class="btn btn-primary" onclick="primaryImage(4107,19373,'/abbie2.jpg'); return false;">
set as profile image
</button>

<button class="btn btn-success" onclick="primaryImage(4107,19374,'/abbie3.jpg'); return false;" disabled="disabled">
profile image
</button>

Please note: the last button is already the active/success button, and i would also like to remove the class on success too (as only one should be active), but that is maybe my next stage....

here is the javaScript, (i have left in some of the methods i have tried, but mented them out)

function primaryImage(eid,pid)
    {
        if (confirm("Are you sure you wish to use this as your profile image?"))
        {

            $.ajax({
                type: "POST",
                async: false,
                cache: false,
                dataType: "json",
                url: "ajax_photo.php",
                data: "action=primary&eid="+eid+"&pid="+pid,
                //context: this,
                success: function(data){
                    if(data.result=='success')
                    {
                        alert('The image is now set as the profile image');
                        //$('button').click(function(){
                        //   $(this).addClass('btn-success');
                        //});

                        //$('button').on(data.result=='success', function(e) {
                        //  $(this).toggleClass("btn btn-success"); //you can list several class names 
                        //  e.preventDefault();
                        //});

                        //$(this).removeClass('btn-primary').addClass('btn-success');

                    }
                    else
                    {
                        alert('An error occurred when trying to set the image: ' + data.result);
                    }
               }
            });
        }
    }

I would be very grateful for any advice of what i am doing wrong (as you can see, i am not too good with JS (yet))

Thanks! Ford

Share Improve this question asked Aug 20, 2015 at 18:56 FordFord 5676 silver badges20 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

As noted in your mented out code, you are binding the click event after click event has already been fired.

I would suggest you to pass a reference of the button that was clicked in the primaryImage() function itself as such:

 <!-- In your HTML -->
 <button class="btn btn-success" onclick="primaryImage(this, 4107,19374,'/abbie3.jpg'); return false;" disabled="disabled">
profile image
</button>

 function primaryImage(button, eid,pid){
  /** ... */

Then using that referenced button, you can add or remove CSS classes to the element, as well as the siblings of the element (using jQuery's siblings() method).

            //your ajax call
            success: function(data){
                if(data.result=='success') //make sure this really is success
                {
                    alert('The image is now set as the profile image');
                    $(button).removeClass('btn-primary').addClass('btn-success');
                    $(button).siblings('button').removeClass('btn-success').addClass('btn-primary');
                }
          }

As you don't use the jQuery .click() event, I think you need to pass the button in your function args.

So your button will look like

<button class="btn btn-primary" onclick="primaryImage(this, 4107,19373,'/abbie2.jpg'); return false;">
    set as profile image
</button>

Then your function will be like

function primaryImage(el, eid,pid)
{

    if (confirm("Are you sure you wish to use this as your profile image?"))
    {

        $.ajax({
            type: "POST",
            async: false,
            cache: false,
            dataType: "json",
            url: "http://anzvirtuel",
            data: "action=primary&eid="+eid+"&pid="+pid,
            //context: this,
            success: function(data){
                if(data.result=='success')
                {
                    $(el).addClass('btn-success');

                    alert('The image is now set as the profile image');

                    // keep doing whatever you want...
                }
                else
                {
                    alert('An error occurred when trying to set the image: ' + data.result);
                }
           }
        });
    }
}

As I have not fully understood your mented JS I'll let you put the code you want, just remember that your button will be accessible in jQuery with $(el).

Hope it may helps you

You should pass the clicked element to the primaryImage() function, and use it on success to do whatever you like.

<button class="btn btn-primary" onclick="primaryImage(this, 4107,19372,'/abbie1.jpg'); return false;">set as profile image</button>

And in your JS

function primaryImage(element, eid,pid)
{
    [...]
         success: function(data){
             if(data.result=='success')
             {
                 $(element).addClass('btn-success');
             }
             else
             {
                 alert('An error occurred when trying to set the image: ' + data.result);
             }
         }
    [...]
}

You could use data-* attributes instead of onclick (MDN Documentation) and then access those throught jQuery, so your code is more clean and HTML / JS are separated.

Try this code, I've created three data attributes (data-eid, data-pid and data-image) for your params and also replaced your JS to make the whole stuff work with those data attributes. Those attributes can be accessed with following jQuery code - var eid = $(this).attr('data-eid'); as an example

This line of code removes the btn-primary class from the clicked button, adds a btn-success class to it and disables it, so it can't be toggled again.

pushedBtn.removeClass("btn-primary").addClass("btn-success").prop("disabled", true);

HTML

<button class="btn btn-primary" data-eid="4107" data-pid="19372" data-image="/abbie1.jpg">
    profile image
</button>
<button class="btn btn-primary" data-eid="4107" data-pid="19373" data-image="/abbie2.jpg">
    profile image
</button>
<button class="btn btn-primary" data-eid="4107" data-pid="19374" data-image="/abbie3.jpg">
    profile image
</button>

JS

$(".btn").click(function (e) {
if (confirm("Are you sure you wish to use this as your profile image?")) {
    var eid = $(this).attr('data-eid'); //like 4107
    var pid = $(this).attr('data-pid'); //like 19372
    var image = $(this).attr('data-image'); //like /abbie1.jpg
    var pushedBtn = $(this);
    $.ajax({
        type: "POST",
        async: false,
        cache: false,
        dataType: "json",
        url: "ajax_photo.php",
        data: "action=primary&eid=" + eid + "&pid=" + pid,
        //context: this,
        success: function (data) {
            if (data.result == 'success') {
                alert('The image is now set as the profile image');
                    pushedBtn.removeClass("btn-primary").addClass("btn-success").prop("disabled", true);


            } else {
                alert('An error occurred when trying to set the image: ' + data.result);
            }
        }
    });
}
});
发布评论

评论列表(0)

  1. 暂无评论