开发者

How could I select all inputs with value greater than 0?

I have a bunch of text inputs with numeric values:

开发者_如何学Python
<input type='text' value='25' />
<input type='text' value='0' />
<input type='text' value='45' />
<input type='text' value='-2' />
.
. etc...

I need to select only those inputs with values greater than 0. How can I do that using jQuery?


Something like this, using .filter():

$('input[type="text"]').filter(function() {
    return parseInt($(this).val(), 10) > 0;
});


//select all text type inputs
$('input[type=text]').each(function(){
    var val = parseInt($(this).val());
    if(val > 0)
        //your logic here
});


Another way, for fun, mostly:

// get array of values
var arr = $('input').map(function() {
    return parseInt(this.value, 10);
}).get();

// use John Resig's uberfast way of getting max value
// http://ejohn.org/blog/fast-javascript-maxmin/
alert(Math.max.apply(Math, arr));

Try it out here.

Note - the above works just fine without parseInt.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜