【问题标题】:form element verification JQuery表单元素验证JQuery
【发布时间】:2011-09-07 21:05:41
【问题描述】:

大家好,我有一个包含 4 个字段的表单 我可以让它检查任何字段是否为空并毫无问题地更改背景颜色,但我遇到的问题是如果所有字段都不为空,则代码不会继续

$(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);
}
});
});
});

【问题讨论】:

    标签: jquery forms verification if-statement


    【解决方案1】:

    当然它永远不会触发。您基本上是将$('.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");
          });
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2016-05-28
      相关资源
      最近更新 更多