document.getElementById返回null

阿里·雷曼王子

当我运行它并提供任何输入数字并按下按钮时,它只会提示一次。为什么是这样?

这是我的代码:

    <html>
    <head> <title> Assignment CS101 </title> </head>
    <script>
    var number;
    var ary;
    var x;  
    function input()
           {
               prompt("Enter","");
               var recieve=document.getElementById('demo');
               number=recieve.value;
               ary = new Array(recieve);
               for(x=0; x<ary.length; x++)
                   {
                     ary[x]=parseInt(prompt("Enter " + x + " name: ",""));
                   }
           }
   </script>
   <input type="text" size="25" id="demo"> <button onclick="input();"> Take input          </button>

用户名

问题是您采用字符串的'demo'的值。并尝试使用该字符串创建一个新的数组。for循环将运行一次,因为您将一个字符串放入其中。

ary = new Array(recieve);

应该使用'parseInt()'

number = parseInt(recieve.value,10);
ary = new Array();
for(var i = 0; i < number; i ++) {

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

document.getElementById()返回null

Javascript Document.getElementById返回null

离子2 document.getElementById返回null

document.getElementById 始终返回 null

所有 document.getElementById('') 返回 null

document.getelementbyid在循环中返回null

为什么document.getElementById在这里返回null?

无法使用document.getElementById获取元素,返回null

document.getElementById返回空

document.getElementById 返回 HTMLDivElement {}

如果未定义element,document.getelementbyId将返回null吗?

如果具有给定ID的元素存在,则document.getelementbyid('ID')方法返回null

开玩笑+酶,使用mount(),document.getElementById()在_method调用后出现的组件上返回null

Vuejs + Google Maps> document.getElementById,this.refs为div返回null

document.getElementById(“ elementId”)。innerHTML =()的倍数,导致第二个返回为null

当存在具有该 ID 的元素时,为什么 document.getElementById() 返回 null?

从document.getElementById()。value获取null

Ajax Uncaught TypeError: document.getElementById(...) is null

document.querySelector返回null

document.getElementById('').value返回未定义

document.getElementById 没有返回任何值

使用 document.getElementById 時返回 [object HTMLDivElement]

TypeError:document.getElementById(...)为null不会停止出现

document.getElementById()!= null在Firefox上不起作用

如何将 document.getElementById(...) is null 设置为 true

在异常TypeError上暂停:document.getElementById(...)为null

即使元素存在 document.getElementById(..) 也会给出 null

我找不到错误?TypeError:document.getElementById(...)为null

TypeError:document.getElementById(...)在javascript中为null