手动表单验证jQuery

时间:2015-06-04 08:39:16

标签: jquery

我试图在jQuery中进行一些手动表单验证,只是为了得到它。 我知道有插件可以做到这一点,但这不是重点。

这是我的代码:

function activaTab(tab){
    $('.nav-tabs a[href="#' + tab + '"]').tab('show');
};

y(document).ready(function () {
  y(document).bind('keydown', 'shift+w', function () {
    activaTab('messages');
  })
});

它突破了以下几行:

$( document ).ready(function() {
//amount of input fields we need this for activation of button
var amount = $('.form-group').length;

  //blur checks when you leave an input field
$('.form-group input').blur( function() {
    //if the input is empty put red bg color
    if( !$(this).val() ) {
        $(this).css("background-color", "red");
    }
     //if field is filled in bg is white and amount -1.
     //once amount is 1 we know fields are filled in
        if( $(this).val() ) {
        $(this).css("background-color", "white");
        amount = amount - 1;
        if(amount === 1 && $('#tos').prop('checked') ) {
          //code to remove the attribute disabled
          $('button').removeAttr("disabled")
          $('button').html("Send")

            }
        }

    });
});
如果我把它们分开编写,但是当我尝试使用&&运营商似乎没有工作。

1 个答案:

答案 0 :(得分:2)

尝试使用其他分组

if((amount === 1) && $('#tos').prop('checked') )

根据operator precedence table===的优先级高于&&,因此您的代码不需要额外的分组,但是.. Hmz。

相关问题