如何使用html5中的javascript在本地存储中使用空格保存数据? [英] How to save data with white spaces in local storage using javascript in html5?

查看:112
本文介绍了如何使用html5中的javascript在本地存储中使用空格保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从变量中的Web服务获取数据。如何使用空格存储数据ex:面包黄油在本地存储中?

Data is fetched from web service in variable. How to store the data with white spaces ex:"Bread Butter" in the local storage?

在函数selected_index中,我已经传递了所单击项目的item_name。
它会在空格之后留下这个词而不存储它。

In the function selected_index I have passed the item_name of the clicked item. It leaves the word after space and does not store it.

这是我的代码。

<script>
var sub_catidall = [];
var sub_catnameall = [];

function selected_index(sub_cat_name_all) {
    alert("selected sub category" + sub_cat_name_all);
    //window.localStorage.setItem("current_sub_id_all",sub_cat_id_all);
    window.localStorage.setItem("DishName", sub_cat_name_all);
    window.open("MenuDetails.html", "_self");
}

function jsondata(data) {
    var parsedata = JSON.parse(JSON.stringify(data));
    var sub_category = parsedata["Item List"];

    for (var i = 0; i < sub_category.length; i++) {
        var sub_menuall = sub_category[i];
        sub_catidall = sub_menuall['menuItemId'];
        sub_catnameall = sub_menuall['menuItemName'];
        //alert(sub_catnameall);

        var id = document.createElement("table");

        if ((i % 2) == 0) {
            id.innerHTML += '<br><td><a href="#" id=' + sub_catnameall + ' onclick="selected_index(this.id)"><strong><font face="DEVROYE">' + sub_menuall['menuItemName'] + '</a></td></strong>';
            document.getElementById("block_left").appendChild(id);

        } else {
            id.innerHTML += '<br><tr><td><a href="#" id=' + sub_catnameall + ' onclick="selected_index(this.id)"><strong><font face="DEVROYE">' + sub_menuall['menuItemName'] + '</a></td></strong>';
            document.getElementById("block_right").appendChild(id);
        }
    }
}

jsonp("http://remoteaddress/hotelTab/menuitem.php?callback=jsondata&mCatId=" + window.localStorage.getItem('current_id') + "&menuCategoryId=" + window.localStorage.getItem('current_sub_id'));
</script>


推荐答案

DOM存储指南

示例

var string = 'Bread Butter';

localStorage.setItem("DishName", string);

alert(localStorage.getItem('DishName'));

On jsFiddle

这篇关于如何使用html5中的javascript在本地存储中使用空格保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆