繁体   English   中英

使用 ajax 内容响应启用和禁用按钮

[英]Enable and disable button with ajax content response

我不明白为什么它不和我一起工作。 我知道使用attrprop启用和禁用按钮。

 $(document).on("click", "#btn", function(){
      $(this).html("Sending...");
      $(this).prop("disabled",true);
      // I have ajax content here. and in success of ajax. run
      $(this).prop("disabled",false);

ajax 成功没有启用按钮disable。 难道我的代码有什么问题。

在ajax内部,不要使用$(this) ,而可以这样做:

$(document).on("click", "#btn", function(){
     var button = $(this);

在ajax内部,则可以使用以下命令:

button.prop("disabled",true);

尝试这个

$(this).attr("disabled", "disabled");

如果要在另一个函数中引用它,则需要保存this的引用:

$(document).on("click", "#btn", function(){
    var button = $(this);
    button.html("Sending...");
    button.prop("disabled",true);
    // I have ajax content here. and in success of ajax. run
    button.prop("disabled",false);

另请参阅: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

AJAX中的$(this)不引用按钮,而是使用:

$('#btn').prop("disabled",false);

使用 async: true 如下:)

$('#button').attr("disabled", true);
$.ajax({
            url: url,
            data: data,
            type: 'post',
            dataType: 'json',
            cache: false,
            async: true,
            complete: function(response){
                $('#button').attr("disabled", false);
            },
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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