使用javascript禁用表单提交按钮


Disable form submit button using javascript

基本上,我在php文件中有一个表单,我想在用户单击提交后禁用提交按钮。如果不是,我的数据库会在多次点击后收到垃圾邮件。

我搜索了stackoverflow&谷歌搜索了我的问题,尝试了很多解决方案,但都没有成功。不确定我是不是很愚蠢。经过无数次的尝试和错误仍然无法提交表格,这就是为什么我制作了一个新的线程。我尝试在表单的提交输入上设置"onsubmit"属性,但表单根本不提交,只是卡住了。还尝试了其他Javascript解决方案,但没有成功。

如有任何帮助,我们将不胜感激!请不要苛刻,我在Javascript方面几乎没有经验。

<form action="adduser.php" method="POST">
              <div class="form_attr">
                <label>Username:</label><input type="text" name="username" value="<?php if ($err_flag == true){echo htmlspecialchars($username);}?>">
                <br/>
                <label>Password:</label><input type="password" name="password" value="">
                <br/>
                <label>Firstname:</label><input type="text" name="firstname" value="<?php if ($err_flag == true){echo htmlspecialchars($firstname);}?>">
                <br/>
                <input type="checkbox" name="admin" value="1"> Admin<br/>
                <input type="checkbox" name="receptionist" value="2"> Receptionist<br/>
                <input type="checkbox" name="technician" value="4"> Technician<br/>
                <input type="submit" name="submit" value="submit">
              </div>
           </form>

这样的东西应该会有所帮助。

$(function(){
    $("input[type='submit'][name='submit']").click(function(){
        $(this).attr("disabled", "disabled");
        return true;
    });
});

演示

<input type="submit"  onclick="this.disabled=true;" name="submit" value="submit" >

没有测试它,但它应该可以工作。

注意:这不是一个好的解决方案-有人可以禁用js并向你发送垃圾邮件。您需要在后端(php中)防止这种情况发生。