码迷,mamicode.com
首页 > 其他好文 > 详细

Input自动补全,并且将输入的数据存放到本地

时间:2014-12-11 19:26:32      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   sp   for   on   数据   div   

<!DOCTYPE HTML PUBLIC
        "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <script src="jquery.js"></script>
        <script src="../bower_components/jquery-ui/jquery-ui.min.js"></script>
        <script src="../bower_components/store.js/store.js"></script>
        <link rel="stylesheet" href="../bower_components/jquery-ui/themes/ui-lightness/jquery-ui.min.css"/>
        <link rel="stylesheet" href="jquery.ui.autocomplete.css"/>
        <script>
            $(function () {
                //todo:id should be an array

                function inputSave(inputTextId, storeId, num) {
                    var ele = $(‘#‘ + inputTextId);
                    if (ele[0].tagName != "INPUT") {
                        return;
                    }
                    var inputValue = $.trim(ele.val());
                    if (inputValue) {
                        storeInput(inputValue, storeId, num);
                    }
                }

                function storeInput(inputValue, storeId, num) {
                    var inputStr = localStorage.getItem(storeId);
                    var inputArr = inputStr ? inputStr.split(‘,‘) : new Array();
                    var idx = $.inArray(inputValue, inputArr);
                    if (idx != -1) {
                        inputArr.splice(idx, 1);
                    }
                    inputArr.unshift(inputValue);
                    if (inputArr.length > num) {
                        inputArr.pop();
                    }
                    localStorage.setItem(storeId, inputArr);
                }

                function autoComplete(inputTextId, storeId, num) {
                    inputSave(inputTextId, storeId, num);
                    var source = localStorage.getItem(storeId);
                    $(‘#‘ + inputTextId).autocomplete({source: source.split(‘,‘)});
                }

                $(‘#saveBtn‘).click(function () {

                    autoComplete("jack", "jack", 10);
                });
                $(‘#clearBtn‘).click(function () {
                    localStorage.removeItem("jack");
                    localStorage.removeItem("zen");
                    console.log(‘clear data ok!‘);

        });
    </script>
    <title></title>
</head>
<body>
<div class="ui-widget">
    <input type="text" id="jack" placeholder="this is test"/>
    <input type="text" id="zen" placeholder="this is test"/>
    <label for="tags">Tags: </label>
    <input id="tags">
    <button id="saveBtn" class="ui-button-text-icons">Save Input Data to Local</button>
    <button id="clearBtn" class="ui-button-text-icons">Clear localData</button>
</div>
</body>
</html>

Input自动补全,并且将输入的数据存放到本地

标签:style   http   io   ar   sp   for   on   数据   div   

原文地址:http://my.oschina.net/sudojs/blog/355146

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!