简体   繁体   English

使用输入字段的值更新JSON对象

[英]update a JSON object with input fields' values

I have created a table to show and update a JSON object. 我创建了一个表来显示和更新JSON对象。 this are 2 fields in that tables. 这是该表中的2个字段。 I have put the JSON path in the name field. 我已将JSON路径放在名称字段中。

var obj = {
        "subscription": {
            "control-data": [
                "app",
                "sp"
            ],
            "callback-url": "http://core.mbv:18080/man-notify",
            "post-notification": true
        },
        "charging-schedulers": [
            {
                "charging-scheduler-name": "fifteen-days",
            }
        ]
    }
<input type="text" name="subscription%$#^control-data%$#^0" class="inputArea" value="app" size="3">

<input type="text" name="subscription%$#^callback-url" class="inputArea" value="http://core.mbv:18080/man-notify" size="36">

I have kept this path to use in updating the JSON object. 我保留了此路径以用于更新JSON对象。 I have tried my own way to update it. 我尝试了自己的方式来更新它。 but it takes hundreds of codes. 但它需要数百个代码。 are there any easy way to update that JSON object. 有什么简单的方法可以更新该JSON对象。

UPDATE UPDATE

I noticed you didn't just want to get the corresponding JSON value but instead wanted to set the value. 我注意到您不只是想要获取相应的JSON值,而是想要设置该值。 I think this will work better. 我认为这会更好。 new jsFiddle 新的jsFiddle

obj = {
    "subscription": {
        "control-data": [
            "app",
            "sp"],
            "callback-url": "http://core.mbv:18080/man-notify",
            "post-notification": true
    },
        "charging-schedulers": [{
        "charging-scheduler-name": "fifteen-days",
    }]
}

function updateJSON() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        var q = inputs[i].name.split("%$#^");
        setValue(obj, q, inputs[i].value);
    }

    // This is the updated JSON obj
    console.log(obj);
}

function setValue(obj, q, val) {
    var sel = q.shift();
    if (typeof obj[sel] === "object" && q.length > 0) {
        setValue(obj[sel], q, val);
    } else {
        obj[sel] = val;
    }
}

HTML HTML

<input type="text" name="subscription%$#^control-data%$#^0" class="inputArea" value="app" size="3">
<input type="text" name="subscription%$#^callback-url" class="inputArea" value="http://core.mbv:18080/man-notify" size="36">
<button id="update" onclick="updateJSON()">Update</button>

You could do something like this. 你可以做这样的事情。 I am assuming your input names correspond to the structure in the JSON object. 我假设您的输入名称与JSON对象中的结构相对应。 jsFiddle 的jsfiddle

var obj = {
    "subscription": {
        "control-data": [
            "app",
            "sp"],
            "callback-url": "http://core.mbv:18080/man-notify",
            "post-notification": true
    },
        "charging-schedulers": [{
        "charging-scheduler-name": "fifteen-days",
    }]
}

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
    var q= inputs[i].name.split("%$#^");
    var value = getValue(obj, q);

    // This is where you have the value for an input
    console.log("value:", value);
}

function getValue(obj, q) {
    var res = obj[q.shift()];
    if (typeof res === "object" && q.length > 0) {
        return getValue(res, q)
    } else {
        return res
    }
}

the function you need is a kind of data binding , and there's a lot of existing libraries to do this. 您需要的功能是一种数据绑定 ,并且有很多现有的库可以执行此操作。 they allow you to declare connections between html form fields and js variables, and synchronize their value dynamically. 它们允许您声明html表单字段和js变量之间的连接,并动态地同步它们的值。

rivets.js seems to be a good option for you, and you may find more similar things by searching js data binding . rivets.js似乎是一个不错的选择,并且您可以通过搜索js数据绑定找到更多类似的东西。

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

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