Javascript 产生误报的jquery验证循环
我正在尝试制作一个mad libs风格的页面,其中有9个文本框用于输入。我遇到了两个问题 首先,当我在所有文本框为空的情况下单击submit时,只有前四个显示错误这是在css中完成的,我在所有文本框上有两个类错误隐藏,我在循环中删除类隐藏以显示错误Javascript 产生误报的jquery验证循环,javascript,jquery,validation,textbox,Javascript,Jquery,Validation,Textbox,我正在尝试制作一个mad libs风格的页面,其中有9个文本框用于输入。我遇到了两个问题 首先,当我在所有文本框为空的情况下单击submit时,只有前四个显示错误这是在css中完成的,我在所有文本框上有两个类错误隐藏,我在循环中删除类隐藏以显示错误 我遇到的第二个问题是,如果我在所有文本框中单击submit with text,则我的验证函数errorcount在每隔一个文本框中最多会出现4个错误。我甚至对索引中的每个文本框都尝试了“$”input.eq0.val.length==0”,每次都返
我遇到的第二个问题是,如果我在所有文本框中单击submit with text,则我的验证函数errorcount在每隔一个文本框中最多会出现4个错误。我甚至对索引中的每个文本框都尝试了“$”input.eq0.val.length==0”,每次都返回false。我不明白如果if then语句不满足参数,它是如何进入的。我不明白你的问题,但是如果输入的验证是空的。。。使用
我知道这不是你想要的,但是这个选择看起来简单多了。
$(document).ready( function()
{
// hides the story and error text when the page loads
$('.errorText').hide();
$("#story").hide();
// global variables for the blanks and the textarea forms
var input = $("form").children();
var storyBlank = $('#story').children();
// Main Event on Click
$('button.submit').on( "click", function (event)
{
// if the form is not validated, highlights errors and prevents the submit from going through
if(!validate())
{
event.preventDefault();
}
// if the form is validated, fills the blanks in the story and displays it
else
{
fillInTheBlanks();
}
});
// Checks to see if there are any empty fields and highlights them if they are empty
function validate()
{
console.log('validate() initiated')
var success = false;
errcnt = 0;
cnt = 0;
while (cnt < 9)
{
if (input.eq(cnt).val().length == 0)
{
errcnt++;
input.eq(cnt).removeClass("hide");
console.log('errorcount', errcnt, 'at input', cnt);
}
else if (input.eq(cnt).val().length !== 0 && !(input.eq(cnt)).hasClass("hide"))
{
input.eq(cnt).addClass("hide");
}
cnt++;
}
if (errcnt == 0)
{
success = true;
}
return success;
}
// Fills in the blanks of the story
function fillInTheBlanks()
{
console.log('fillInTheBlanks() executed');
var blankCount = 0;
while (blankCount < 9)
{
storyBlank.eq(blankCount).empty().append(input.eq(blankCount).val());
blankCount++;
}
$("#story").show();
}
});