javascript数组推送不起作用

时间:2017-05-05 01:47:54

标签: javascript arrays push

这是我的jS代码。当我按下按钮时,我想添加另一个值为1的元素。现在它正在重置数组,所以我永远不会得到多个元素。我该怎么办?

 var x = document.getElementsByTagName('button');//return button array
 var age_array = [];
  smoker_array = [];
  relation_array = [];

  age = 0;

//add button clicked
x[0].addEventListener("click", function(){

   /*
   var age = document.getElementsByName("age")[0].value;//pull age from box
   var relation = document.getElementsByName("rel")[0].value;//pull relation
   let smoker = document.querySelector('[name=smoker').checked;

   //check relation 
   if(relation === "")
   {
       alert("please select a realation"); 
   }

   //check to see if age < 0
   if(age < 0 || age === " ")
   {
       alert("age not applicable");
   }
    */


  age_array.push(1);
  alert(age_array.length);


});

/*function submit(age, relation, smoker)
{
   age_array.push(age);

    alert(age_array[0]);
    alert(age_array[1]);
    /*
    x[1].addEventListener("click", function(){
       var age = JSON.stringify(entry_age);
       alert(entry_age[1]);
       document.getElementbyClassName("debug").innerHTML = JSON.stringify(entry_relation);
       document.getElementByClass("debug").innerHTML = JSON.stringfy(entry_smoker);  
    });
}
    */

这是html代码

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Household builder</title>
        <style>
            .debug {
                font-family: monospace;
                border: 1px solid black;
                padding: 10px;
                display: none;
            }
        </style>
    </head>
    <body>
        <h1>Household builder</h1>
        <div class="builder">
            <ol class="household"></ol>
            <form>
                <div>
                    <label>Age
                        <input type="text" name="age">
                    </label>
                </div>
                <div>
                    <label>Relationship
                        <select name="rel">
                            <option value="">---</option>
                            <option value="self">Self</option>
                            <option value="spouse">Spouse</option>
                            <option value="child">Child</option>
                            <option value="parent">Parent</option>
                            <option value="grandparent">Grandparent</option>
                            <option value="other">Other</option>
                        </select>
                    </label>
                </div>
                <div>
                    <label>Smoker?
                        <input type="checkbox" name="smoker">
                    </label>
                </div>
                <div>
                    <button class="add">add</button>
                </div>
                <div>
                    <button type="submit">submit</button>
                </div>
            </form>
        </div>
        <pre class="debug"></pre>
        <script type="text/javascript" src="index.js"></script>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

我不知道是不是原因,但您的代码无法在严格模式下运行

[Customer]:
Load [TranDate], [ID], [StatusID]
FROM [lib://user/test2.xlsx]
(ooxml, embedded labels, table is Customer);

应该是:

var age_array = [];
    smoker_array = [];
    relation_array = [];

或:

var age_array = [],
    smoker_array = [],
    relation_array = [];

答案 1 :(得分:0)

嘿,伙计们感谢您的帮助。 HTML似乎有问题。我不知道是什么。我让JS使用自己的HTML工作,我不允许更改HTML。无论如何,感谢所有人的帮助。

将此标记为已解答