浏览器验证具有"required"属性的字段后,如何禁用提交按钮? [英] How to disable submit button AFTER browser has validated fields with 'required' attribute?

查看:186
本文介绍了浏览器验证具有"required"属性的字段后,如何禁用提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用以下jQuery禁用提交按钮,以防止重复提交(服务器速度慢!).

We are using the following jQuery to disable submit buttons in order to prevent double submissions (slow server!).

jQuery(document).ready(function($){

    /*SENDE BTN INAKTIV STELLEN*/
    $("input[type=submit]").click(function() {
        $(this).css({'opacity':'0.5','cursor':'wait'}).attr('disabled','disabled');
    });

});

但是,在浏览器用HTML表单中的"required"属性验证了所有字段之后,我们如何触发脚本?

However, how do we fire the script AFTER the browser has validated all fields with the 'required' attribute in our HTML form?

推荐答案

EDIT: I found a much more elegant way to do this. Please accept this answer if it works for you.

 jQuery(document).ready(function($){

/*SENDE BTN INAKTIV STELLEN*/
    $("input[type=submit]").click(function() {

        var myform = $('#theform');

        //checks if the form is valid
        if(myform[0].checkValidity())
        {
             $(this).css({'opacity':'0.5','cursor':'wait'}).attr('disabled','disabled');
        }
    });

});


//this is slightly awkward but it works

jQuery(document).ready(function($){

/*SENDE BTN INAKTIV STELLEN*/
$("input[type=submit]").click(function() {
    var flag = true;

    $('[required]').each(function(){
        if(!$(this).val()) 
           flag = false;
    });

    if(flag){
       $(this).css({'opacity':'0.5','cursor':'wait'}).attr('disabled','disabled');
    }
    else
       return false;
});

});

这篇关于浏览器验证具有"required"属性的字段后,如何禁用提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆