<!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>
高洛峰2017-05-18 10:56:19
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梦2017-05-18 10:56:19
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