使用Jquery检查输入中的数字

时间:2014-01-07 10:33:43

标签: javascript jquery html

我正在尝试使用jquery创建一个函数。而我想要做的是,我有一个文本字段和提交按钮所以当我插入数字值从1到10它打开一个链接如果输入值是11-20按钮点击它应该打开不同的链接。

HTML

<input type="text" Id="number" />
<input type="submit" id="button" />

jquery

 $( document ).ready(function() {
   if ( $("#number").val() >= 19200 && num <= 19276 ) {
    $( "#button" ).click(function() {
      alert( "u enterd the number " );
    });  
   }
 });

http://jsfiddle.net/2nppc/1/

5 个答案:

答案 0 :(得分:3)

你需要这些东西:

$(document).ready(function(){
    $('#button').click(function(){
         var number = $('#number').val();
         //check if number
         if($.isNumeric(number)){
             if(number >= 1 && number <= 10){
                 //1-10 action
             }elseif(number >= 11 && number <= 20){
                 //11-20 action
             }
         }else{
             //Not a number
         }
    });
});

编辑:抱歉混淆了检查数字的格式,现在排序:)

答案 1 :(得分:1)

使用此代码:

$( document ).ready(function() {
    $( "#button" ).click(function() {  
      var num = $("#number").val();
      if(!isNAN(num)){
      if (num >= 1 && num <= 10) {
         window.location.href="your url for 1 to 10";
      } else if(num >= 11 && num <= 20) {
         window.location.href="your url for 11 to 20";
      } else {
         //your other code or action
      }
      } else {
         alert("Enter the numeric value");
      }
    });  
});

答案 2 :(得分:0)

没有测试:

    $(function(){
       $('#button').on('click', function(){
         if ($('#number').val() >= 0 && $('#number').val() <= 10)
           location.href = 'blabla';
         else if ($('#number').val() > 10 && $('#number').val() <= 20)
           location.href = 'blabla';
       });
    });

答案 3 :(得分:0)

 <script>
      if ( parseInt($("#number").val()) <11) {
         window.location.href = '/link1';
      } 
      else{
        window.location.href = '/link2';
      }
</script>

答案 4 :(得分:0)

 $( document ).ready(function() {
          $( "#button" ).click(function() {   // Replace with Id of the button
          var num = $("#number").val();       // Replace with Id of the textbox
          if(!isNaN(num)){
          if (num >= 1 && num <= 10) {
            window.location.href="your url for 1 to 10";
          } else if(num >= 11 && num <= 20) {
            window.location.href="your url for 11 to 20";
          } else {
            //your other code or action
          }
         }
     });  
 });
相关问题