Json操作

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Js/jquery-1.9.1.js"></script>
    <script type="text/javascript">
        //公有变量
        var Datas = {};
        //点击事件
        function checkMe(obj) {
            var key = $(obj).attr("value");
            var keyInfo = $(obj).attr("info");
            var isNot = true;
            for (var i in Datas) {
                if (i == key) {
                    delete Datas[i];
                    isNot = false;
                }
            }
            if (isNot) {
                $(Datas).attr(key, keyInfo);
            }
        }
        //输出变量
        function showInfo() {
            for (var i in Datas)
                alert(i);
        }
        //设置选中
        function setCheck() {
            var argss = ["李四", "王五", "李三"];
            $.each(argss, function (i, n) {
                var o = "input:checkbox[value=" + n + "]";
                $(o).attr("checked", true);
            });
        }
    </script>
</head>
<body>
    <input type="button" value="验证" onclick="showInfo()" />
    <input type="button" value="设置" onclick="setCheck()" />
    <div>
        <ul>
            <li>
                <input type="checkbox" value="李四" id="checkbox2" onclick="checkMe(this)" info='{"年龄":"22"}' /><label for="checkbox2">李四</label></li>
            <li>
                <input type="checkbox" value="王五" id="checkbox3" onclick="checkMe(this)" info='{"年龄":"33"}' /><label for="checkbox3">王五</label></li>
            <li>
                <input type="checkbox" value="赵六" id="checkbox4" onclick="checkMe(this)" info='{"年龄":"44"}' /><label for="checkbox4">赵六</label></li>
            <li>
                <input type="checkbox" value="李三" id="checkbox5" onclick="checkMe(this)" info='{"年龄":"22"}' /><label for="checkbox5">李三</label></li>

        </ul>
    </div>
</body>
</html>

 

posted @ 2014-11-30 23:33  ~Apple  阅读(365)  评论(0编辑  收藏  举报