简体   繁体   English

AJAX调用后无法禁用javascript中的按钮

[英]Cannot disable button in javascript after AJAX call

I have the following js file and I want to disable a button after my AJAX call: 我有以下js文件,我想在AJAX调用后禁用按钮:

$('document').ready(function() {
  const runField = $('input[id=run]')
  const runButton = $('.btn-run')
  const saveButton = $('.btn-save')

  runButton.on('click', function(event) {
    console.log('run clicked')
    runField.val(1)
    event.preventDefault()
    $('.btn-run').prop('disabled', true)
    console.log($('.btn-run').prop('disabled'))
    runCode()
    $('.btn-run').prop('disabled', false)
  })
})

runCode() is my AJAX function here, but when I used .prop('disabled', true) the button was still not disabled, even though the following logged line returned true in my console. runCode()是我的AJAX函数,但是当我使用.prop('disabled', true) ,即使下面的日志行在控制台中返回true,该按钮仍未被禁用。 I made sure all the properties like ID's are correct. 我确保所有属性(例如ID)都是正确的。 Any help appreciated! 任何帮助表示赞赏!

You need to use a promise or a callback function for $('.btn-run').prop('disabled', false) - sharing your runCode would help if possible. 您需要为$('.btn-run').prop('disabled', false) runCode或回调函数-如果可能,共享runCode会有所帮助。

You AJAX callback should look something like below. 您的AJAX回调应如下所示。 URL will be different, but $('.btn-run').prop('disabled', false) is being run on success. URL将有所不同,但是$('.btn-run').prop('disabled', false)成功运行。 This should mean it will take effect. 这意味着它将生效。

$.ajax({
    url: "demo_test.txt", 
    success: function(result){
        $('.btn-run').prop('disabled', false)
    }
});

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

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