【发布时间】:2015-09-24 00:21:10
【问题描述】:
我有以下代码:
//Populate inputData with the values of the input text boxes, in order
var inputData = {};
var x = document.getElementById("newInventoryForm");
//Added this following line in the edit
$('#additionalContent').append("My x.length property " + x.length + "<br/>");
for (var i = 0; i < x.length - 1; i++) {// -1 so we don't get the submit button
var addData = {
id : x[i].id,
value : x[i].value
};
inputData[i] = addData;
$('#additionalContent').append(inputData[i].id + " : " + inputData[i].value + "<br/>");
}
我正在尝试将表单数据从以前制作的表单传递到另一个 javascript 函数。在我这样做之前,我试图让它在同一个 .js 页面中工作,但是当我尝试通过 for 循环输出我的 inputData 时,它显示为空白。我确定这是因为inputData.length 是undefined。我的印象是,将其声明为 inputData = {}; 使其成为一个数组,因此具有默认长度值。我怎样才能将其更改为正确的长度?
编辑
一些评论者说var x = document.getElementById("newInventoryForm"); 应该返回 null 或一个节点,它们都没有长度属性。在上面的代码中添加这一行,就产生了这个输出。
我的 x.length 属性 18 序列号:456 someDatafield : someInput
【问题讨论】:
标签: javascript razor model-view-controller