简体   繁体   English

Javascript / JSON错误:格式不正确

[英]Javascript/JSON error: not well formed

I am testing putting a text editor on my page and storing it as part of a JSON object. 我正在测试将文本编辑器放在页面上,并将其存储为JSON对象的一部分。

HTML 的HTML

<!DOCTYPE html>
<html>
    <head>
        <script src="http://tinymce.cachefly.net/4.0/tinymce.min.js" type="text/javascript">      </script>
        <script type="text/javascript">
            tinymce.init({
            selector: "textarea"
            });
        </script>
        <link rel="stylesheet" href="/jquery.mobile-1.3.2.min.css"/>
        <script src="/jquery-1.9.1.min.js"></script>
        <script src="/jquery.mobile-1.3.2.min.js"></script>
    </head>
    <body>
        <form method="post" action="formSubmit.js">
            <textarea name ="editor"></textarea>
            <p><input type="submit" value="Submit"></p>
        </form>
    </body>
</html>

JS JS

$(document).ready(function () {
    var text = $("editor").val();
    var name = "project name";
    var id = 5;
    var item = new item(name, text, id);
    var itemArray = localStorage.items;
    if (itemArray == undefined) {
        itemArray = [];
    } else {
        itemArray = JSON.parse(itemArray);
    }
    itemArray.push(item);
    localStorage.items = JSON.stringify(itemArray);
});

I want to be able to store item in a JSON object. 我希望能够将item存储在JSON对象中。 When I run this I receive a "not-well formed" error at line 1 of the Javascript. 当我运行它时,我在JavaScript的第1行收到“格式不正确”的错误。 It's a very simple program I'm running and can't seem to pinpoint what is causing the error. 这是我正在运行的一个非常简单的程序,似乎无法查明是什么导致了错误。 Is the JSON done incorrectly or are scripts in my HTML header causing issues? JSON是否正确处理?还是HTML标头中的脚本引起了问题?

$("editor") is looking for an html tag called 'editor'. $(“ editor”)正在寻找一个名为'editor'的html标签。 you probably want to attach an id attribute to your and do $('#editor') 您可能想将id属性附加到您的对象上,然后执行$('#editor')

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

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