javascript - 我如何在ajax函数中使用$this?

标签 javascript jquery ajax laravel

所以这是我的问题:

我有这两行带有图像和确认按钮

我想要的是当我点击那个按钮时,另一个按钮的行为与被点击的按钮不同。

when i click (我也不希望它们都发生加载)

结局如何

我想要什么

查看:

<a id="executarJob1" href="#">
    <img class="jobexecutadoimg1" src="{{ URL::to('/icons/donetarefa.png') }}">
    <img class="jobexecutadogif1" width="25" height="18" src="{{ URL::to('/icons/loading.gif') }}">
</a>
<img class="jobexecutadoimg2" src="{{ URL::to('/icons/donetarefacomplete.png') }}">

<a id="executarJob2" href="#">
    <img class="jobexecutadoimg1" src="{{ URL::to('/icons/donetarefa.png') }}">
    <img class="jobexecutadogif1" width="25" height="18" src="{{ URL::to('/icons/loading.gif') }}">
</a>
<img class="jobexecutadoimg2" src="{{ URL::to('/icons/donetarefacomplete.png') }}">

我的ajax代码:

$(document).ready(function () {

    $(".jobexecutadoimg2").hide();
    $(".jobexecutadogif1").hide();

    $("#executarJob1").on('click', function () {
        $.ajax('{{route("executar",$lnd->id_demanda)}}',
            {
                beforeSend: function (carregando) {
                    $('.jobexecutadoimg1').html(carregando).hide();
                    $('.jobexecutadogif1').html(carregando).show();
                },
                success: function (finalizado) {
                    $('.jobexecutadoimg2').html(finalizado).slideDown('slow').show()
                    $('.jobexecutadoimg1').html(finalizado).hide();
                    $('.jobexecutadogif1').html(finalizado).hide();
                }
            });
    });
});

图片名称让你们知道正在处理的是哪一个

  • jobexecutadoimg1 = 蓝色按钮
  • jobexecutadoimg2 = 绿色按钮(完成)
  • jobexecutadogif1 = Gif 按钮(加载中)

如何在我的 ajax 中使用 $this?谢谢。

最佳答案

我不认为你需要使用 this 来让它工作

尝试:

$(document).ready(function () {

$(".jobexecutadoimg2").hide();
$(".jobexecutadogif1").hide();

function flipImgs(id)
{

    $.ajax('{{route("executar",$lnd->id_demanda)}}',
        {
            beforeSend: function (carregando) {
                $(id +' .jobexecutadoimg1').html(carregando).hide();
                $(id +' .jobexecutadogif1').html(carregando).show();
            },
            success: function (finalizado) {
                $(id +' .jobexecutadoimg2').html(finalizado).slideDown('slow').show()
                $(id +' .jobexecutadoimg1').html(finalizado).hide();
                $(id +' .jobexecutadogif1').html(finalizado).hide();
            }
        });
}
//you can create a loop for the folowing code
$("#executarJob1").on('click', flipImgs("#executarJob1") );
$("#executarJob2").on('click', flipImgs("#executarJob2") );
}

关于javascript - 我如何在ajax函数中使用$this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934107/

相关文章:

javascript - 选择隐藏的元素并使用 jQuery 操作它们

jquery - 将 AJAX 返回数据转换为 JSON

javascript - 如何在 javascript 调用后重新加载样式?

javascript - 多个ajax调用同一个文件来执行同一个文件中的不同代码?

javascript - 如何使用 Javascript 将 "div"移动到某些坐标?

javascript - 使用绿色 socks 将旋钮拖动到 360 度

javascript - 左右箭头键对我不起作用

javascript - 将 JSON 从 PHP Controller 传递到 AngularJS Controller 的最佳方法?

javascript - 如何检查页面上的所有 vimeo 视频是否都是通过单击按钮播放的?

jquery - 正则表达式在 jquery 验证插件中不起作用