admin管理员组文章数量:1326461
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
5 Answers
Reset to default 4You'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';
});
});
本文标签: javascriptchecking numbers from input using JqueryStack Overflow
版权声明:本文标题:javascript - checking numbers from input using Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742209076a2433366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论