禁用提交,直到表单填写javascript

时间:2013-07-03 19:12:13

标签: javascript forms validation

我需要禁用提交按钮,直到所有字段都填写规则任何提示?

window.onload = $("input[type=submit]").attr("disabled", "disabled");
$(function(){   
                    $("input[type=submit]").attr("disabled", "disabled");
                    var total = document.getElementById('valor_total'),
                    descontado = document.getElementById('valor_descontado'),
                    valor_final = document.getElementById('valor_final'),
                    vendedor = document.getElementById('vendedor'),
                    cliente = document.getElementById('cliente'),
                    no_contrato = document.getElementById('contrato'),
                    validation;

                        var f_total = total.value;
                        var f_descontado = descontado.value;
                        var f_final = valor_final.value;
                        var f_vendedor = vendedor.value;
                        var f_cliente = cliente.value;
                        var f_no_contrato = no_contrato.value;

                        $("#numero_contrato").blur(function() {

                        if ( f_vendedor == "0" || f_cliente == "0" || f_no_contrato == "" || f_total == "0,00" || f_final == "0,00") {
                        validation = false;
                        } else {
                        validation = true;
                        }
                        if (validation = true) {
                        $("input[type=submit]").removeAttr("disabled"); 
                        } else {
                        $("input[type=submit]").attr("disabled", "disabled");
                        }
                        });
                });

我做错了什么?

我想在id为numero_contrato的字段中输入该用户类型,该函数运行并启用或不启用提交

1 个答案:

答案 0 :(得分:1)

对于初学者,请尝试修复此条件:

if (validation === true) {
     $('input[type=submit]').removeAttr('disabled'); 
} else {
     $('input[type=submit]').attr('disabled', 'disabled');
}

你有一个等于用于分配的等号。你想要双倍或者最好是三倍等于。但是你可以完全删除它们,因为你使用的是布尔值: if(validation){...}