推送到JSON文件

时间:2018-05-22 07:59:36

标签: javascript json

早上好,我尝试使用命令“MyJSON.name.push”将临时值推送到JSON文件,但它告诉我:“未定义它不是对象”。我尝试了一些表单和javascript数组工作,但我需要在外部JSON文件上。仅当我单击某个按钮时才会出现错误。有人可以帮帮我吗? 谢谢。 HTML:

<!DOCTYPE html>
<html>

<head>

    <title>SSL Checker</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/script.js"></script>
    <script type="text/javascript" src="js/json.json" charset="utf-8"></script>
</head>

<body onLoad="start()">
    <div id="title">
        <h1>SSL Checker</h1>
    </div>
    <div id="data">
        <form action="javascript:void(0);" method="POST" onsubmit="SSL.Add()">
            <input type="text" id="add-name" placeholder="Name"></input>
            <input type="text" id="add-link" placeholder="Link"></input>
            <input type="submit" value="Add">
        </form>

        <div id="edit" role="aria-hidden">
            <form action="javascript:void(0);" method="POST" id="saveEdit">
                <input type="text" id="edit-name">
                <input type="submit" value="Edit" /> <a onclick="CloseInput()" aria-label="Close">&#10006;</a>
            </form>
        </div>
        <p id="counter"></p>
    </div>
    <div id="table">
        <table style="overflow-x:auto;">
            <tr>
                <th>Sites:</th>
            </tr>
            <tbody id="urls">
            </tbody>
        </table>
    </div>
</body>


</html>

js:

function start() {
var SSL = new function() {
            //List urls to check
            this.el = document.getElementById('urls');
            this.Count = function(data) {
                var el = document.getElementById('counter');
                var name = 'url';

                if (data) {
                    if (data > 1) {
                        name = 'urls';
                    }
                    el.innerHTML = 'There are:' + ' ' + data + ' ' + name;
                } else {
                    el.innerHTML = 'No ' + name;
                }
            };
            //Buttons configuration
            this.FetchAll = function() {
                var data= '';

                if (MyJSON.length > 0) {
                    for (i = 0; i < MyJSON.length; i++) {
                        data += '<tr>';
                        data += '<td><a href="' + MyJSON[i].url + '">' + MyJSON[i].name+ '</a></td>';
                        data += '<td><button onclick="SSL.Edit(' + i + ')">Edit</button></td>';
                        data += '<td><button onclick="SSL.Delete(' + i + ')">Delete</button></td>';
                        data += '</tr>';

                    }
                }

                this.Count(MyJSON.length);
                return this.el.innerHTML = data;
            };
            //Add name
             this.Add = function() {
            el = document.getElementById('add-name');
            el1 = document.getElementById('add-link')
            var url = el.value;
            var url1 = el1.value;
            if (url) {
                if (url) MyJSON.push('{name:"url",url:"url1"}')
                el.value = '';
                this.FetchAll();
            }
        }                
            //Edit
            this.Edit = function(item) {
                var el = document.getElementById('edit-name');
                el.value = MyJSON.name[item];
                document.getElementById('edit').style.display = 'block';
                self = this;
                document.getElementById('saveEdit').onsubmit = function() {
                    var url = el.value;
                    if (url) {
                        self.name.splice(item, 1, url.trim());
                        self.FetchAll();
                        CloseInput();
                    }
                }
            }; 
           //Delete
            this.Delete = function(item) {
                MyJSON.urls.splice(item, 1);
                this.FetchAll();
            };

        };

        SSL.FetchAll();

        function CloseInput() {
            document.getElementById('edit').style.display = 'none';
        }
    window.SSL= SSL;
}

JSON FILE(json.json):

var MyJSON = [{
        name:"Google",
        url: 'google.es',
    },
    {
        name:"Yahoo",
        url: 'yahoo.com',
    }
]

1 个答案:

答案 0 :(得分:0)

像这样更改this.Add()功能:

this.Add = function() {
        el = document.getElementById('add-name');
        el1 = document.getElementById('add-link')
        var url = el.value;
        var url1 = el1.value;
        if (url) {
            MyJSON.push({"name":url,"url":url1})
            el.value = '';
            this.FetchAll();
        }
    }