为什么获取到的是undefined?
<!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.name+" 性别:"+json.sex+"</li>"); }); }); </script> </body> </html>
求解