简体   繁体   English

禁用和启用表单提交按钮

[英]Disabling and Enabling a form submit button

I have been trying to create a bit of javascript that will disable a submit button that is a anchor tag when the page loads then when all the form inputs and textareas are filled in it will be enabled and the class of the button will also need to change when its enabled. 我一直在尝试创建一些JavaScript,当页面加载时将禁用作为锚标签的提交按钮,然后在所有表单输入和文本区域均被填充时将启用该按钮,并且该按钮的类也需要启用时更改。

I have had a few unsuccessful of coding this and found a jquery way of doing it but that didn't work because im using a anchor tag instead of a input button 我对此进行了一些失败的编码,并找到了执行此操作的jquery方式,但这没有用,因为我使用锚点标签而不是输入按钮

To disable any a tag: 要禁用任何a标签:

$('a').click(function(event)
{
  event.preventDefault();
});

if you know id or class of the anchor tag then use $('#aId') o $('.aId') respectively 如果您知道锚标记的ID或类,则分别使用$('#aId')o $('。aId')

If you don't want to use jQuery: 如果您不想使用jQuery:

var anchors = document.querySelectorAll('a');
for (var i = 0; i < anchors.length; i++ )
    anchors[i].onclick = function(event)
    {
     event.preventDefault();   
    }​​​

You have to prevent the triggering of the link click-event. 您必须防止触发链接单击事件。

Using jQuery that would be something like: 使用jQuery类似于:

$('a.disabled').on('click', function(event){
    event.preventDefault();
});

So when the form is ready to be submit you could run the following: 因此,当准备好提交表单时,您可以运行以下命令:

$('a.disabled').toggleClass('disabled enabled');

This causes the button to be enabled and now has the class .enabled 这将导致按钮被启用,并且现在具有.enabled.enabled

In jQuery, you can simply do that by return false 在jQuery中,只需return false

$("#disable_button").click(function(){
   //somethings you need
   return false;
});

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

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