Javascript 返回布尔值的jquery错误
我是javascript新手,无法返回函数的布尔值。。我正在验证文本框是否为null,如果其帮助为空,请返回falseJavascript 返回布尔值的jquery错误,javascript,jquery,Javascript,Jquery,我是javascript新手,无法返回函数的布尔值。。我正在验证文本框是否为null,如果其帮助为空,请返回false validateForm : function() { $('.requiredField :input').each(function(){ if ('input[type=text]'){ if($(this).val().length === 0){ $(this).addClass(
validateForm : function() {
$('.requiredField :input').each(function(){
if ('input[type=text]'){
if($(this).val().length === 0){
$(this).addClass('warning');
var errorMsg = $('<br /><span>Please Fill in the TextBox</span>').addClass('warning');
errorMsg.insertAfter(this);
$(errorMsg).css('color','red');
$('.warning').css('border-color','red');
//$(this).focus(function(){
//$(this).removeClass('warning');
//$(this).parent().children('span').remove();
//$(this).parent().children('br').remove();
//});
return false;
}
else
return true;
}
});
},
Form.validateForm(); // call to the function
validateForm:function(){
$('.requiredField:input')。每个(函数(){
if('input[type=text]”){
if($(this).val().length==0){
$(this.addClass('warning');
var errorMsg=$(“
请填写文本框”).addClass('warning');
errorMsg.insertAfter(此);
$(errorMsg.css('color','red');
$('.warning').css('border-color','red');
//$(this.focus(function()){
//$(this.removeClass('warning');
//$(this.parent().children('span').remove();
//$(this.parent().children('br').remove();
//});
返回false;
}
其他的
返回true;
}
});
},
Form.validateForm();//对函数的调用
您正在从.each()
内部返回ing。这不会使函数返回值
在.each()
循环中,返回false
就像使用break
,并且返回true
类似于使用continue代码>
您需要在.each()
之外声明一个变量,在循环内设置其值,然后在循环后返回它。检查此行
if ('input[type=text]'){
应该是
if($('input[type=text]')){
您可以尝试以下方法:
$('.requiredField :input').each(function(){
var i=$(this).val();
if(i == '' || i == null)
{
//execute your codes
return false;
}else{
return true;
}
});
看起来你在尝试写一个插件?请尝试以下代码:
(function($) {
$.fn.validateForm = function()
{
var formID = $(this).attr('id');
$('#'+ formID +' input[type=submit]').click(function(e)
{
e.preventDefault();
$('input[type=text]').each(function(){
if($(this).val().length === 0){
$(this).addClass('warning');
var errorMsg = $('<span>Please Fill in the TextBox</span>').addClass('warning');
errorMsg.insertAfter(this);
$(errorMsg).css('color','red');
$('.warning').css('border-color','red');
return false;
}else{
return true;
}
});
});
};
})(jQuery);
(函数($){
$.fn.validateForm=函数()
{
var formID=$(this.attr('id');
$(“#”+formID+“输入[type=submit]”)。单击(函数(e)
{
e、 预防默认值();
$('input[type=text]')。每个(函数(){
如果($(this).val().length==0){
$(this.addClass('warning');
var errorMsg=$(“请填写文本框”).addClass(“警告”);
errorMsg.insertAfter(此);
$(errorMsg.css('color','red');
$('.warning').css('border-color','red');
返回false;
}否则{
返回true;
}
});
});
};
})(jQuery);
正如@RocketHazmat所提到的,您的函数需要从内部循环聚合结果,并有一个退出点,以便验证(并添加css类/html元素)每个输入
您需要这样做:
validateForm : function () {
var invalidInputs = [];
// only test the type='text' inputs
$('.requiredField :input[type="text"]').each(function () {
// if there is no value
if ($(this).val() == undefined || $(this).val().length == 0) {
$(this).addClass('warning');
var errorMsg = $('<br /><span>Please Fill in the TextBox</span>').addClass('warning');
errorMsg.insertAfter(this);
$(errorMsg).css('color','red');
$('.warning').css('border-color','red');
// add the invalid input to an array
invalidInputs.push($(this));
}
});
// The form is only valid for no invalid inputs were found.
return invalidInputs.length == 0;
}
validateForm:函数(){
var=Puts[];
//仅测试type='text'输入
$('.requiredField:input[type=“text”]')。每个(函数(){
//如果没有价值
if($(this).val()==未定义的| |$(this).val().length==0){
$(this.addClass('warning');
var errorMsg=$(“
请填写文本框”).addClass('warning');
errorMsg.insertAfter(此);
$(errorMsg.css('color','red');
$('.warning').css('border-color','red');
//将无效输入添加到数组
无效的push($(this));
}
});
//表单仅在未找到无效输入时有效。
return.length==0;
}
您是否收到错误消息?@Kaf无消息或无错误返回false到何处?你只是在执行这个函数,具体的问题是什么?