jquery取input值
<script type="text/javascript">
$(document).ready(function(){
$("#add").click(function(){
var userName =$("#userName").attr("value");
var age =$("#age").attr("value");
alert(userName);
//这里取不到,点击后是undenfind
var user ={userName:userName,age:age};
$.ajax({
url:"/springmvc/user/data/addUserJson",
type:"get",
data:user,
success:function(data){
alert("userName--->"+data.userName+"age--->"+data.age);
}
});
});
});
</script>
</head>
<body>
<h1>json添加用户</h1>
姓名:<input type="text" id="userName" name="userName" >
年龄:<input type="text" id="age" name="age" >
<input type="button" value="添加" id="add">
</body>
</html>
------解决方案--------------------var userName =$("#userName").val();
var age =$("#age").val();