douyaosi3164 2016-11-18 21:09
浏览 80
已采纳

Jquery根据.load请求的输出禁用或启用操作按钮

Me and another programmer are writing a web chat application. Previously he wrote the chat program for application only use, and private use due to some bugs that if found and released publicly, could destroy the system. The users apart of said system, were trust worthy enough that if they found the bugs, would not exploit them and bring his servers down. I begged him to write code that could implement into PHP, and told him i'd write the front-end for it.

However. I've run into an issue regarding jquery. Sure we can run the website pure html/css/php and have constant page reloading... OR, we can mix jquery into the equation and make this easier on users.

TL;DR: I'm writing a chat application with jquery, and this function that is meant to disable the chat functions if the chat loader denies them, doesn't work.

  setInterval(function(){
    $('#reload').load('/file.php?server=serverfur');
    if($('#reload').html() == "Login to view server logs"){
      if($(".login-button").prop('disabled')){
        $(".login-button").prop('disabled', 'false');
        $(".logout-button").prop("disabled", 'true');
      }
    }else{
      if($(".login-button").prop('disabled')){
        $(".login-button").prop('disabled', 'false');
        $(".logout-button").prop("disabled", 'true');
      }
    }
  }, 1000);
  • 写回答

1条回答 默认 最新

  • duan0427 2016-11-18 21:47
    关注

    .load() is asynchronous, so you're checking the result before it finishes. You need to put that code into the callback function.

      setInterval(function(){
        $('#reload').load('/file.php?server=serverfur', function() {
            if($('#reload').html() == "Login to view server logs"){
              if($(".login-button").prop('disabled')){
                $(".login-button").prop('disabled', false);
                $(".logout-button").prop("disabled", true);
              }
            }else{
              if($(".login-button").prop('disabled')){
                $(".login-button").prop('disabled', false);
                $(".logout-button").prop("disabled", true);
              }
            }
        });
      }, 1000);
    

    Also, to enable/disable a button, the disabled property should be set to a boolean, not a string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器