Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果值小于最小值,如何设置输入的数据最小值和设置消息_Javascript - Fatal编程技术网

Javascript 如果值小于最小值,如何设置输入的数据最小值和设置消息

Javascript 如果值小于最小值,如何设置输入的数据最小值和设置消息,javascript,Javascript,我试图设置输入的最小值,但消息未显示。如果需要输入且输入的值小于最小值,如何显示此消息。 我的代码是: $(“.mydiv:input”).each(函数(){ var min=$(this.attr('datamin'); if(min!==未定义的&&min!==空&&min!==假){ 如果(值

我试图设置输入的最小值,但消息未显示。如果需要输入且输入的值小于最小值,如何显示此消息。 我的代码是:

$(“.mydiv:input”).each(函数(){
var min=$(this.attr('datamin');
if(min!==未定义的&&min!==空&&min!==假){
如果(值<最小值){
有效=错误;
$(this.addClass('error');
$(this).closest('label').addClass('red');
$(this).closest('label').html('Required min value:'+min);
}否则{
$(this.removeClass('error');
$(this).closest('label').removeClass('red');
}
}


*
您需要实际检索值:

var theValue = +$(this).val(); //you're not doing this
//note that + sign attempts to coerce a value (vs. text), but you may try some additional validation
if(theValue < min) {
      valid = false;
      $(this).addClass('error');
      $(this).closest('label').addClass('red');
      $(this).closest('label').html('Required min value:' + min);
   } else {
      $(this).removeClass('error');
      $(this).closest('label').removeClass('red');
   }
}
var theValue=+$(this.val();//您没有这样做
//请注意,+符号试图强制一个值(相对于文本),但您可以尝试一些额外的验证
如果(值<最小值){
有效=错误;
$(this.addClass('error');
$(this).closest('label').addClass('red');
$(this).closest('label').html('Required min value:'+min);
}否则{
$(this.removeClass('error');
$(this).closest('label').removeClass('red');
}
}

编辑:正如@potatopeelings所添加的,您还需要在
change

上触发此事件。您需要将此事件附加到
change
事件,而不是在每个事件上执行。您还需要设置值(使用
.val()

转换和围绕值进行更多验证也会很好(使用
isNaN

$(文档).ready(函数(){
$(“.mydiv输入”).on('change',function(){
var min=Number($(this.attr('datamin'));
var值=数字($(this).val());
如果(min!==未定义&&min!==空&&min!==假&&&isNaN(min)){
如果(值<最小值| isNaN(值)){
有效=错误;
$(this.addClass('error');
$(this).closest('label').addClass('red');
$(this).closest('label').html('Required min value:'+min);
}否则{
$(this.removeClass('error');
$(this).closest('label').removeClass('red');
}
}
})
});
.error,.red{
颜色:红色;
}

*

检查您的web控制台。最后缺少一个大括号
}
。不,现在是因为复制了代码。没关系。我认为问题是datamin属性,对吗?