无法从javascript中的数组内的字符串中删除单引号

时间:2017-01-05 04:48:37

标签: javascript replace single-quotes

我试图从已经字符串化的对象输出键值对。但只要检测到单个引号,数据就会停止输出。

这就是我得到的:

[{"name":"john

但是想看看:

[{"name":"john's world"}]

这是javascript代码:

$(function() {

    var allObjects = [

        {
            name: "john's world",
            msg: "hello world"
        }, {
            name: "billy",
            msg: "goodbye"
        }
    ]

    apiResults(allObjects);

    function apiResults(processedItems) {

        var output = '';

        output += "<form class='add-item-form' method='get'>";
        output += "<label for='item-input'>Enter a name to save your list and press ENTER</label><br><br>";
        output += "<input type='text' id='item-input' name='item-input' title='Item' required autocomplete='off' placeholder='my wine list' autofocus>";
        output += "<input type='hidden' id='search-results-array'  name='search-results-array' value='" + JSON.stringify(processedItems) + "'>";
        output += "<button id='submitBtn' type='submit'>Add item</button>";
        output += "</form><br>";

        output += "<div class='container'>";
        $('#results').html(output);
        console.log(processedItems);
    }

    $(document).on('submit', '.add-item-form', function(event) {
        event.preventDefault();

        if (event.type === 'keypress' && event.which === 13 || event.type === 'submit') {


            // searchArray is attached to the api result
            var searchArray = $('#search-results-array').val();
            console.log("searchArray", searchArray);

        }

    });
});

这是问题的一个方面: https://jsfiddle.net/marcusk1/mpahzfw4/15/

感谢您的帮助。

0 个答案:

没有答案