【发布时间】:2018-08-23 04:10:25
【问题描述】:
这是我的问题:我有两个输入字段,单击按钮后会触发一个函数,以使用这两个输入值创建一个新对象(来自对象构造函数)。
函数中包含的console.log以正确的方式显示object.input1和object.input2。但是,一旦功能完成,我再次尝试控制台记录新创建的对象 OR object.input1 OR object.input1,我得到'未定义'
如果我将新创建的对象推送到数组中,没关系,我可以从数组中访问对象,但不确定如何在数组中访问对象元素。无论如何,我不明白我在创建新对象时做错了什么。任何帮助将不胜感激。
<html>
<input type="number" id='input1' placeholder="Enter number only">
<input type="text" id='input2' placeholder="Max length 300" maxlength='300'>
<button id='btn'>Submit</button>
<script>
var day1, notes1, day1List;
function DayList(day, notes) {
this.day = day;
this.notes = notes;
}
document.getElementById('btn').onclick =
function() {
var day1 = document.getElementById('input1').valueAsNumber;
var notes1 = document.getElementById('input2').value;
var day1List = new DayList(day1, notes1);
console.log(day1List.day, day1List.notes);
}
</script>
</html>
【问题讨论】:
标签: javascript