开发者社区 问答 正文

javascipt 有关 input value 和 innerHtml 一问? 400 报错

javascipt 有关 input value 和 innerHtml 一问? 400 报错

<html>
<script>
    var submit = function(){
        if(!isNan(document.getElementById('num').value))){
            /*do something*/
        }else{
            /*do another something*/
        }
    }
</script>
<input id='num' type='text'></input>
<button id='submit' type='buton' onclick='submit()'>sumbit</button>
</html>

我想让用家输入到id='num'的input,然后再判断是否数字,再根据情况做其他的事,可是这样的代码做不到我想要的效果,请问哪里出错了?谢谢。

展开
收起
爱吃鱼的程序员 2020-05-29 19:30:38 358 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    isNaN(). 另外dom取出来的值貌似都是字符串的在转换之前不要直接去判断是否数字
    ######

    好像isNan不是这样写的,应该是这样写isNaN,

    还有就是document.getElementById('num').value得到的值是一个字符串,是否应该转换一下

    document.getElementById('num').value*1

    ######

    谢谢两位大大~
    原来是isNaN的问题
    另外我试过了,document.getElementById('num').value不需要转换
    谢谢两位,两位也答得很好,谢谢

    2020-05-29 19:30:40 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答地址: