【发布时间】:2012-08-02 04:09:37
【问题描述】:
可能重复:
Is there a (built-in) way in JavaScript to check if a string is a valid number?
我有一个 HTML 输入搜索栏
<form class="form-inline" action="javascript:displayResult()">
<input id="searchKey" >
<button onclick="result()" type="button" class="btn btn-warning btn ">
Go
</button>
</form>
这是Javascript函数
function result() {
search($('#searchKey').val());
if(typeof(searchKey)=="number") { // checking if this is a number
someFunction();
}
};
问题是,对于搜索栏中的每个条目,我都会将值作为字符串获取,即使搜索栏中的值是“hello”或 9789。我使用alert(typeof(searchKey)); 进行验证,它总是将类型返回为字符串
我试图在搜索栏中区分数字和字符串,我确信有更好的方法可以做到这一点,但我不确定为什么这不起作用
我不能使用 parseInt() 因为我需要动态区分文本和数字
【问题讨论】:
-
return语句之后的代码何时执行? -
你在return man后调用它!
-
抱歉,我已经编辑了我的代码
-
searchKey是全局变量吗? -
尝试 parseInt(str, base) 如果失败,它应该只返回 0
标签: javascript jquery html