简体   繁体   中英

is there any way to know which button was clicked?

I have been making ajax requests many times. I need to know which button I clicked

    <button class="ajax-btn" onclick="function_1();">Ajax 1</button>
    <button class="ajax-btn" onclick="function_2();">Ajax 2</button>
    ...
    <button class="ajax-btn" onclick="function_10();">Ajax 10</button>
    // ajax request for each button

        var ajaxBtnVal = ""; // keep button text

        $(document).ajaxStart(function(e, xhr, options) {
            $(".ajax-btn").addClass('spinner spinner-white spinner-right disabled');
            ajaxBtnVal = $(".ajax-btn").text();
            $(".ajax-btn").text('Processing...');
            // which button was clicked ?
        });

        $(document).ajaxComplete(function() {
            $(".ajax-btn").removeClass('spinner spinner-white spinner-right disabled');
            $(".ajax-btn").text(ajaxBtnVal);
        });

This should show you which button was clicked.

$('.ajax-btn').click(function () {
    console.log($(this).index('.ajax-btn'));
});
window.onclick = function (e) {
    window.clickedButton = $(e.target);
};

$(document).ajaxStart(function (e, xhr, options) {
    window.clickedButton.addClass('spinner spinner-white spinner-right disabled');

    ajaxBtnVal = $(".ajax-btn").text();
    window.clickedButton.text('Processing...');
    // which button was clicked ?
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM