最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - checking numbers from input using Jquery - Stack Overflow

programmeradmin0浏览0评论

I am trying to make a function using jquery. And what i am trying to do is that i have a text field and submit button so when i insert numeric value from 1 to 10 it open up a link if the input value is from 11-20 on button click it should open different link.

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 " );
    });  
   }
 });

/

I am trying to make a function using jquery. And what i am trying to do is that i have a text field and submit button so when i insert numeric value from 1 to 10 it open up a link if the input value is from 11-20 on button click it should open different link.

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/2nppc/1/

Share Improve this question edited Jan 7, 2014 at 10:41 Umar Khan asked Jan 7, 2014 at 10:33 Umar KhanUmar Khan 4548 silver badges30 bronze badges 4
  • How u want to open the link? On submit button click? – Developer Commented Jan 7, 2014 at 10:37
  • First thing you can do is check wether the data entered is numerical or not. Check isNaN() (javascript) or .isNumeric() (jQuery) functions. – TCHdvlp Commented Jan 7, 2014 at 10:37
  • 1 You should move the if statement to the click handler. – Ram Commented Jan 7, 2014 at 10:39
  • @ParthoGanguly yes I want to open the link on submit button – Umar Khan Commented Jan 7, 2014 at 10:40
Add a ment  | 

5 Answers 5

Reset to default 4

You're going to need something along these lines:

$(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
         }
    });
});

EDIT: sorry mixed up the format for checking numeric, sorted now :)

use this code:

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

Without testing:

    $(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';
       });
    });
发布评论

评论列表(0)

  1. 暂无评论