Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 如何在jquery中使用if语句检查输入值是否大于12_Javascript_Jquery - Fatal编程技术网

Javascript 如何在jquery中使用if语句检查输入值是否大于12

Javascript 如何在jquery中使用if语句检查输入值是否大于12,javascript,jquery,Javascript,Jquery,如何在jquery中使用if语句检查输入值是否大于12 var time = $('#assessTime'); $(time).keypress(function(){ debugger; if($(this).val() > 12){ alert("hello"); } }); 试试这个 var time = $('#assessTime'); $(time).keyup(function(){ debugger;

如何在jquery中使用if语句检查输入值是否大于12

 var time = $('#assessTime');
 $(time).keypress(function(){
     debugger;
     if($(this).val() > 12){
     alert("hello");
     }
 });
试试这个

 var time = $('#assessTime');
 $(time).keyup(function(){
     debugger;
     if(Number($(this).val()) > 12){
     alert("hello");
     }
 });
它将限制设置为12。

1。请检查以下代码以检查给定输入值是否大于12

修正案1:

修正案2:


现在,当前代码出了什么问题?对不起,我误解了这个问题。根据要求,当前代码可以正常工作。您现有的代码可以工作,但您希望使用“keyup”事件而不是“keypress”与keypress。您在用户输入之前获取值,在用户输入之后获取值。@Teemu是的,您是正确的Teemu。在这种情况下,我们可以使用数字$this.val。如果我错了,请纠正我。是的,那个或一元加号或parseFloat就行了。
<input type="number" value="0" />

$('input').on('input', function () {

var value = $(this).val();

  if ((value !== '') && (value.indexOf('.') === -1)) {

    $(this).val(Math.max(Math.min(value, 12), -12));
  }
});
 var time = $('#assessTime');
 $(time).keypress(function(){
     if(parseInt($(this).val()) > 12){
         alert("Yes the given input value is Greater than 12..");
      }
 }); 
 var time = $('#assessTime');
 $(time).keypress(function(){
     if(Number($(this).val()) > 12){
         alert("Yes the given input value is Greater than 12..");
      }
 });