简体   繁体   English

JavaScript:如何防止双击按钮单击或禁用按钮

[英]JavaScript: how to Prevent Double button click or disable the button

I have a webpage where multiple user information details is there.so when i click on the complete (here complete button is there beside every user details) button it should do some task(eg. I'm sending mail here) and disable the button after i redirected to same page again. 我有一个网页,其中有多个用户信息详细信息。因此,当我单击“完成”(每个用户详细信息旁边有“完成”按钮)时,它应该执行某些任务(例如,我在此处发送邮件)并禁用该按钮在我再次重定向到同一页面后。 How do I do it? 我该怎么做? Any possible suggestion? 有什么建议吗?

[ It's a blade template as i'm making laravel web application] [这是我正在制作laravel Web应用程序时的刀片模板]

A demo of my webpage: 我的网页演示:

<html>
    <head> 
        <title> User Details </title>

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> 

    </head>
    <body>


<div class="container">
<h3> Student Details </h3>
      <table  class="table table-striped table-bordered"  id="example">
        <thead>
          <tr>
            <td>Serial No</td>
            <td>Student Name</td>
            <td>Stdunet Email</td>
            <td>Course Name</td>  
            <td>Phone Number</td>
          <td>Action</td>
        </tr>
        </thead>
        <tbody>
          <?php $i=1; ?>
        @foreach($user as $row)
        @if($row->courses()->first())
          <tr>
            <td>{{$i}}</td>
            <td>{{$row->name }}</td>
            <td>{{$row->email}}</td>
           <td>{{$row->courses()->first()->name}} </td>
            <td>{{$row->phone}}</td>
           <td>

               <a href="{{route('sendMail',$row->id)}}" class="btn btn-warning">Complete</a>                                        

            </td>
          </tr>
          <?php $i++; ?>
          @endif


        @endforeach
        </tbody>


      </table>

    </div>

    </body>
</html>

You can use jquery's .one() method. 您可以使用jquery的.one()方法。

 // prevent anchor to click $(document).ready(function() { var clickCtr = 0; $("a.btn").click( function() { if(clickCtr > 0) { return false; } clickCtr++; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <a href="https://www.google.co.in" target="_blank" class="btn btn-warning">Complete</a> 

So there is couple ways to handle your issue. 因此,有几种方法可以解决您的问题。

A simple one from the client side would be to use jQuery (since your importing it in your HTML) to temporarily disable the button. 从客户端来看,一个简单的方法就是使用jQuery(因为您已将其导入HTML)暂时禁用了该按钮。

Here is an exemple: 这是一个例子:

 $(document).ready(function () { clicked = false; $(".btn").on('click', function (event) { if (!clicked) { clicked = true; $(".text").text("Clicked!"); setTimeout(function(){ clicked = false; $(".text").text(""); }, 2000); } else { $(".text").text("Already clicked!"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button class="btn btn-warning">Complete</button> <text class="text"></text> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM