dongyo1959 2019-07-10 11:45
浏览 59
已采纳

禁用按钮呼叫服务器,然后重新启用按钮

I've seen many similar questions to mine, but have spent a few hours browsing and can't find the answer to my situation.

I have a Go based web-server. I have an HTML template page that lets me update the hardware clock and system clock on a 'nix system with the current browser time:

enter image description here

The update button is of type submit and calls the Go code which makes some system calls which take a few seconds, and I can't have this server code being called twice whilst it's still processing the first request.

So I want to disable the update button and then re-enable it once the server has responded.

I have jQuery, so I know I can have a function that is called via `onclick' and make these calls:

$("#updateButton").prop("disabled", true);
$("#updateButton").prop("disabled", false);

I have the form being submitted via the button (and not a jQuery call for instance - although I'm all ears) because I want the form to refresh when the server responds. So I have this:

<form action="/clock" method="POST">
    ...
</form>

This is so some values on the page get updated. Such as the status message at the top and the values in the various fields (updated to the values actually set).

The first problem I have is that if I call a JS function on the button click, and it disables the button as per above, then my server code doesn't get called! I have no idea why - could this be stopping the post request somehow?

<script>
function getTime() {
    // Disable update button while waiting for response
    $("#updateButton").prop("disabled", true); // Stops post request???

    // How to re-enable update button?

}
</script>

And secondly, how do I re-enable the button? I don't know how to tell when the server has responded.

  • 写回答

1条回答 默认 最新

  • dthswrp84966 2019-07-10 22:19
    关注

    Since you're saying you want to disable the submit button once the button is clicked. And you're also saying you want the page to refresh while the form is being submitted.

    <form action="" method="post" onsubmit="subBtn.disabled = true; return true;">
        <input type="submit" name="subBtn" value="submit"/>
    </form>
    

    In the code above, when <form> is submitted, <input> field named subBtn is disabled and true is returned for <form> to continue the submission process. Once the page is reloaded, the button goes to its original state, which is enabled.


    I am not sure whats going on the code down there or how/when is getTime() function executed, but return true; might just submit the form.

    <script>
    function getTime() {
        $("#updateButton").prop("disabled", true);
        return true; // this to continue with the original <form> process
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题