【发布时间】:2017-10-16 04:34:52
【问题描述】:
我有一个函数循环遍历表单的所有输入并检查它们是否已填写。如果该字段为空白,则该特定输入变为粉红色并返回 false。
我正在尝试在未填写的输入下方添加“必填字段”消息。所以我在每行之后编写了一个额外的表格行,其中包含一个包含错误消息的 div。 div 的 css 在页面加载时设置为“display:none”。
现在我的函数对每个输入都显示为“必需”,而不仅仅是空白输入,而且粉红色仍然正常工作。
如何让“必需”的 div 像粉红色一样正确显示和隐藏?
checkinputs = function (blockOne,blockTwo) {
inputGood = true;
blOne = $(blockOne);
blTwo = $(blockTwo);
blInput = [blOne,blTwo];
for (x = 0; x < 2; x++) {
var validators = blInput[x].find(" [id$='RequiredIndicator']").parent().parent('tr').find(':input:not(:hidden)');
var notAllFilled = validators.filter(function(){
var myInput = $(this); //.parent().parent('tr').find(':input');
var filledVal = myInput.val();
var isFilled = $.trim(filledVal).length;
if (isFilled) {
$(this).css('background-color', 'white');
$(this).closest('div').find('.required').hide();
$(this).parent('td').prev('td').find('span').text('*');
}
else {
$(this).css('background-color', 'pink');
$(this).closest('div').find('.required').show();
$(this).parent('td').prev('td').find('span').text('*');
inputGood = false;
}
return isFilled;
}).length;
var inputCount = validators.length;
};
if( !inputGood ){
$('#errorAlert').append('<span style="font-weight:bold;">"Some required information is missing. Please complete the missing fields below."</span>' + '<br><br>');
$('#errorAlertTwo').append('<span style="font-weight:bold;">"Some required credit card information is missing. Please complete the missing fields below."</span>' + '<br><br>');
}
return inputGood;
};
这里有一个问题: http://jsfiddle.net/RNMM7/
【问题讨论】:
-
请显示你的html标记,同时显示完整的js代码,$(this)指的是什么?
-
如果你能做个小提琴就更好了!
-
这段 javascript 代码没有 HTML 就毫无意义。也发布您的 HTML
-
对不起,添加了一个问题!
-
该死,我的 Fiddle 没有像它应该的那样触发函数。
标签: javascript jquery forms html-table show-hide