开发者

problem count numbers

I created a characters generator.

From: &#38;&#35;<input type="text" value="33" class="from" />;
To: &#38;&#35;<input type="text" value="1030" class="to" />;

It has two input fields to enter two numbers with max difference of 1000.

var from = $('input.from').val();
var to = $('input.to').val();
var diff = (to - from);

if ( diff > 1000 ){        
    $('.error').html('ERROR: This code could run slow. Numbers range MAX 1000 !');
}

It works fine but when in the right (to) field are numbers in the range 100 - 1000 if won't work.

Could be the later peace of code is messing the things? (I 开发者_JAVA技巧populate with it the container with characters in the range of the selected numbers, olways if theirs difference is not bigger than 1000 ('cause than the script may become unstable if more than 1000 generated divs.))

    var ascii = '';
    for (var i = from; i <= to; i++) {
        ascii += "<div class='box'> &#38;&#035;"+i+"&#59;  <span>&#"+i+";</span> </div>"
    }


Typecast your inputs as numbers or else the for loop won't work correctly :)

var from = Number($('input.from').val());
var to = Number($('input.to').val());

http://jsfiddle.net/8hV8E/4/


Change loop to

for (var i = parseInt(from,10); i <= parseInt(to,10); i++)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜