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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。