2011-09-07 74 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"); 
     }); 
}