javascript - Asking for advice: What is the correct way to use $each()?
天蓬老师
天蓬老师 2017-05-18 10:54:19
0
2
708
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <form>
            <label>
                <input id="txt" type="text" name="UserName" placeholder="请输入姓名">性别:
                <select id="sex">
                    <option>女</option>
                    <option>男</option>
                </select>
            </label>
            <input id="show-btn" type="button" value="显示数据">
        </form>
        <ul id="list"></ul>

    <script>
        var json=[{
            "name": $("#txt").val(),
            "sex": $("#sex").val()
        }];
        $("#show-btn").click(function(){
            $.each(json,function(i){

                $("#list").append("<li>姓名:"+json[i].name+" 性别:"+json[i].sex+"</li>");    

            });
        });

    </script>
    </body>
</html>

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
小葫芦

Please put var json=[xxxxx] ;
into the click function

    $("#show-btn").click(function(){
      var json=[{
        "name": $("#txt").val(),
        "sex": $("#sex").val()
    }];
        $.each(json,function(i){

            $("#list").append("<li>姓名:"+json[i].name+" 性别:"+json[i].sex+"</li>");    

        });
    });
过去多啦不再A梦

Did the poster not describe the problem clearly? I thought you were talking about the usage of each

==================================================== =====
Do you mean you don’t want to use subscripts in callbacks?

The above picture is JQuery’s each method
You can use this in the callback to get the object currently looped to
or use the second parameter in the callback

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!