用jQuery解析 [英] parseInt with jQuery

查看:90
本文介绍了用jQuery解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我弄清楚为什么以下jQuery代码不起作用吗? 我想从用户输入中返回一个整数.

Can someone help me figuring out why the following jQuery code doesn't work? I want to return a integer from an user input.

var test = parseInt($("#testid"));

谢谢&再见!

推荐答案

var test = parseInt($("#testid").val(), 10);

您必须告诉它想要输入的目标value.

You have to tell it you want the value of the input you are targeting.

而且,总是向parseInt提供第二个参数(基数).它会尝试变得过于聪明,如果未提供,则会自动检测到它,并可能导致意外结果.

And also, always provide the second argument (radix) to parseInt. It tries to be too clever and autodetect it if not provided and can lead to unexpected results.

提供10假定您想要一个以10为底的数字.

Providing 10 assumes you are wanting a base 10 number.

这篇关于用jQuery解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆