简体   繁体   English

一键使用javascript如何禁用引导按钮

[英]How to disable bootstrap button after one click using javascript

How do I disable a Bootstrap button after it is clicked using Javascript. 使用Javascript单击后如何禁用Bootstrap按钮。 I am using the onclick event but it doesn't get disabled. 我正在使用onclick事件,但不会被禁用。

Code: 码:

<div class="panel-heading">
    <div class="btn-group pull-right"> 
         <a href="/assign" class="btn btn-success">Assign</a>
         <a href="#" class="btn btn-primary" onclick="this.disabled=true">Upload</a>
   </div>
</div>
$("#buttonid").on("click", function() {
    $(this).prop("disabled", true);
});

add

$(this).prop("disabled",true);

to you click event function 给您单击事件功能

The following solution can be used when an element (eg button, or link) is rendered as an A (anchor) tag (eg command link-buttons in ASPX pages). 当元素(例如按钮或链接)呈现为A (锚)标签(例如ASPX页面中的命令链接按钮)时,可以使用以下解决方案。 (Anchors do not have an (HTML) disable attribute, so cannot simply be disabled by setting that attribute. Slightly confusingly, Bootstrap will make the button appear to be disabled, but in practice another click (or worse still, double-clicks) will cause an on-click or href (where this is actually "javascript:...") to be re-invoked.) (锚点没有(HTML)禁用属性,因此不能简单地通过设置该属性来禁用。有点令人困惑的是,Bootstrap将使该按钮看上去已被禁用,但是实际上,再次单击(或者更糟糕的是,双击)导致点击或href(实际上是“ javascript:...”)被重新调用。)

NB: Needs jquery. 注意:需要jQuery。

  1. Add the script from the jsfiddle reference below, to your master page or individual pages. 将以下jsfiddle参考中的脚本添加到您的母版页或单个页中。

  2. Apply the disableafteroneclick class to any button rendered as an anchor (A) where you want to restrict second/double clicks disableafteroneclick类应用于要限制第二次/双击的任何显示为锚点(A)的按钮

  3. Optionally, add to the a/button data-disabledtext attribute (this will replace the text on the button after the first click. (可选)添加到a / button data-disabledtext属性(这将替换第一次单击后按钮上的文本。

NB: The disabled nature of the button will only be removed when the page is re-rendered - so this is mostly used where (for example) a submit button which must be click only once, will move the user to another page. 注意:仅当重新渲染页面时,按钮的禁用性质才会被删除-因此通常用于(例如)必须单击一次的提交按钮将用户移至另一页面的情况。

You'll see I've used the lines: 您会看到我使用了以下行:

if ($(this).attr("href")) window.location = $(this).attr("href");
return false;

for the first click (where the invocation is needed) - which might have been replaced with simply: 第一次点击(需要调用)-可能已被简单地替换为:

return true;

...but discovered that this doesn't work for IE <= 8 - and our clients need to provide support for IE8! ...但是发现这不适用于IE <= 8的IE-我们的客户需要为IE8提供支持! If you know you won't need that, then you certainly could use the simplified code created by that replacement. 如果您知道不需要它,那么您当然可以使用该替换创建的简化代码。

Code is at: https://jsfiddle.net/robertgalesorguk/qbq1n369/4/ 代码位于: https : //jsfiddle.net/robertgalesorguk/qbq1n369/4/

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

相关问题 一键单击后如何禁用提交按钮? - How to disable a submit button after one click? 第一次单击javascript后如何禁用按钮 - how to disable button after first click in javascript 无论如何使用PHP在服务器端单击一次后永久禁用引导按钮? - Is there anyway to disable a bootstrap button permanently after one click on server side using PHP? 如何使用Javascript在点击时禁用HTML按钮? - How to disable HTML button on the click using Javascript? 使用 JavaScript 在 phaser.io 中单击后禁用单击 - Disable click after one click in phaser.io using JavaScript 单击引导模式中的“保存”按钮后如何禁用刷新页面? - How to disable refresh page after click on Save button in bootstrap modal? 如何使按钮禁用,并从响应中返回错误消息,以及使用angular2单击一次后 - How to make button disable with error message from response and after one click using angular2 一键单击后如何禁用暂停按钮,并使用angular2和typescript在弹出窗口显示后立即启用 - How to disable the pause button after one click and enable as soon as popup displays using angular2 and typescript 如何在单击后禁用金属 UI 的按钮? - How to disable button of metarial UI after one click? 单击 1 后如何禁用按钮? - How to Disable a Button after 1 click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM