简体   繁体   English

对象数组未显示所有变量

[英]object array not showing all variables

I have the code shown below. 我有下面显示的代码。 My problem is: the part console.log(obj) is saying that Object {InternalNumber = 22 } and leaving out all the other variables. 我的问题是: console.log(obj)部分说的是Object {InternalNumber = 22 }而忽略了所有其他变量。 I am expecting it to say: 我期望它说:

Object { Id = someID, ParameterId="someParaId", InternalNumber = someNr, value="someValue"}

What might be wrong? 可能是什么问题?

If you haven't noticed... I am saving the object to localStorage, and then retrieving it from there. 如果您没有注意到...,我将对象保存到localStorage,然后从那里检索它。

function getModel() {

    var model = {
        Id: '',
        ParameterId: '',
        InternalNumber: '',
        Value: ''
    }

    return model;
}

function saveObjectToLocal() {
    model = getModel();

    model.Id = $(this).find(':input[name$=Id]').val();
    model.ParameterId = $(this).attr('id');
    model.InternalNumber = currentParcel.children('#viewModel_InternalNumber').val();
    model.Value = $(this).find(':input[name$=Value]').val();

    localStorage.setItem("model", JSON.stringify(model));        
}

function getObjectFromLocalAndInsertInFields() {
    obj = JSON.parse(localStorage.getItem("model"));
    console.log(obj);
}

How are you calling saveObjectToLocal function. 您如何调用saveObjectToLocal函数。 $(this) inside of that function is probably not matching anything because "this" is probably the global (window) object, and DOM elements aren't matched within the window object. 该函数内部的$(this)可能不匹配任何内容,因为“ this”可能是全局(窗口)对象,而DOM元素在window对象中不匹配。

To see what I'm talking about. 看看我在说什么。 Run: 跑:

$(this);
$(this).find("input");
$(this).find("input").attr("id");

from your console. 从您的控制台。 The first output will be length 1 of just window, the second would be an empty jQuery object, and the third would be undefined. 第一个输出将是窗口的长度1,第二个将是一个空的jQuery对象,第三个将是未定义的。

Calling .val() and .attr on an empty jQuery list would be undefined and therefore not serialized to JSON. 在空的jQuery列表上调用.val()和.attr将是未定义的,因此不会序列化为JSON。 InternalNumber is serialized because currentParcel.children is giving a match. InternalNumber被序列化,因为currentParcel.children正在给出匹配项。 You need to fix the $(this) selectors. 您需要修复$(this)选择器。

Json Stringify函数将排除具有未定义值的属性,因此请首先检查缺少的属性是否具有值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM