2011-09-07 76 views
0

大家好我有一个具有4个领域 我能得到它来检查任何字段为空,并更改没有问题的背景颜色,但我得到的问题是,如果所有的字段是形式不是空的它不是继续的代码表单元素验证JQuery的

$(document).ready(function(){ 
$("#submitpw").click(function(){ 
var un = $("#FirstName").val(); 
var pw = $("#LastName").val(); 
var sid = $("#StudentID").val(); 
var ss = $("#LastFour").val(); 
var unl = un.substring(0,un.length - un.length + 1); 
var pwl = pw.substring(0,pw.length - pw.length +4) 
var emptyTextBoxes = $('.information').filter(function() { return this.value == ""; }); 
emptyTextBoxes.each(function() { 
if ($('.information:empty')) 
{ 
$(this).css("background-color", "red"); 
alert("Please Enter Information in the Red Fields"); 
} 
else 
{ 
("This is the part that never triggers!!!") 
alert("it worked"); 
$("#campusun").html(unl+pwl+sid+ss); 
$("#campuspw").html(sid+ss); 
$("#emailun").html(unl+pwl+sid+"@live.tcicollege.edu"); 
$("#emailpw").html(sid+ss); 
} 
}); 
}); 
}); 

回答

0

当然,它永远不会触发。你基本上是组合在一起的一组从$('.information')元素,说你拿到3个元素。该each方法是要调用每个元件上的回调;因为$('.information')必须为非空,甚至调用回调,和你的那张支票代码在调用的回调,它永远不会满足的条件。

这听起来像你想这样做:

if(emptyTextBoxes.length == 0) 
{ 
     alert("it worked"); 
     $("#campusun").html(unl+pwl+sid+ss); 
     $("#campuspw").html(sid+ss); 
     $("#emailun").html(unl+pwl+sid+"@live.tcicollege.edu"); 
     $("#emailpw").html(sid+ss); 
} 
else 
{ 
     emptyTextBoxes.each(function() 
     { 
      $(this).css("background-color", "red"); 
      alert("Please Enter Information in the Red Fields"); 
     }); 
}