javascript - 如何禁用 html 按钮 jquery

标签 javascript php jquery html ajax

我正在尝试使用 jquery 禁用按钮,以便用户无法多次单击该按钮,但它不起作用,代码让我一遍又一遍地重新单击注册按钮!我做了研究并找到了解决方案,但似乎可以让它们适合我的场景!

这是我的 JavaScript $(文档).ready(函数(){

 $('#button').click(function(e){

 $('#button').prop('disabled',true);


var setIt = 'set' ;
$.post("form.php",{pull_form: setIt},function(data){

        $('.msg').fadeOut('slow',function(){
          $(this).html(data);
          $('.red').hide();
          $('.all').hide();
          $('.red').fadeIn('slow');
          $('.all').fadeIn('slow');
        }).fadeIn('slow');




        $('.red').click(function(){
        $('.all').css('display','none') ;
          })

})


 e.preventDefault() ;
 })




}) ; 

这是我的 html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Using GET</title>
    <link rel="stylesheet" href="css.css" media="screen" type="text/css">
  </head>
  <body>

<h1>Click the button to Register now</h1>
<button type="button" >

  <a id='button' href="register.php">Register</a>

</button>


<div class="msg">

</div>



<div class="msg2">

</div>

<div class="all">

<button type="button" class="red">X</button>

</div>
<script src='../jquery.js'></script>
<script type="text/javascript" src='custom.js'></script>

  </body>
</html>

这是我的 ajax 正在调用的文件

    <?php

    if(isset($_POST['pull_form'])) {


      ?>
    <div class="all">
    <h2>Register</h2>




    <form class="" action="index.html" method="post">


      <input type="text" name="name" value="">
      <input type="text" name="password" value="">
      <input type="submit" name="submit" value="Submit">

    </form>

    </div>
    <?php
    }

     ?>

最佳答案

首先,您可以更改您的#button 元素。它不是一个按钮。您只能使用 a 标签。

并关注:

<form class="Myfrom" action="your_action_page.php" method="post">
      <input type="text" name="name" value="">
      <input type="text" name="password" value="">
      <input type="submit" id="submit-button" name="submit" value="Submit">
</form>

$(".Myfrom").on("submit", "#submit-button", function(e){
     e.preventDefault() ;
     $(this).prop('disabled',true);
});

关于javascript - 如何禁用 html 按钮 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40144953/

相关文章:

php - PHP 如何检测 session 超时?

php - 使用循环从多个 PHP 回显属性设置 CSS x 和 y

javascript - 编写隐藏/显示 div 代码的更短、更简单的方法

javascript - VueJS如何在使用下拉选择时过滤数组数据

javascript - DIV 不显示在与变量的链接上

javascript - 执行列排序后 dgrid 无法保留滚动位置

javascript - 切换 div,如果已经打开则隐藏

php - 将行中的数据放入无 php 站点进行编辑

javascript - 如何根据div标签图像自动调整图像大小

javascript - 在 Chrome 中,触发 $(document).ready() 时不会加载 <embed> 资源。为什么?