【问题标题】:Disable form submit button using javascript使用javascript禁用表单提交按钮
【发布时间】:2015-12-07 01:49:56
【问题描述】:

基本上我在 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>

【问题讨论】:

标签: javascript php jquery html


【解决方案1】:

这样的事情应该会有所帮助。

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

Demo

【讨论】:

  • 谢谢,我已经尝试了您的解决方案,我可以让按钮禁用,但表单没有发布。有什么想法吗?
  • 您的代码没有错误,但是经过一番搜索,我找到了一个可行的解决方案! stackoverflow.com/questions/6634999/…
【解决方案2】:
<input type="submit"  onclick="this.disabled=true;" name="submit" value="submit" >

没有测试,但应该可以。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2018-10-12
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多