hh493296076
csdn_yzm
采纳率0%
2016-08-01 09:28 阅读 1.9k

ajax的异步提交到action,成功插入数据库在页面返回结果

jsp代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



注册
<br><br> function checkUserName(obj){<br><br> var username = obj;<br><br> var checkUserNameResult = document.getElementById(&quot;checkUserNameResult&quot;);<br><br> if(username.trim().length==0){<br><br> checkUserNameResult.innerHTML = &quot;*用户名不能为空&quot;;<br><br> obj.focus();<br><br> }else{<br><br> checkUserNameResult.innerHTML = &quot;&quot;;<br><br> }<br><br> }<br><br>
<br><br> function checkPassword(obj){<br><br> var password = obj;<br><br> var checkPasswordResult = document.getElementById(&quot;checkPasswordResult&quot;);<br><br> if(password.trim().length==0){<br><br> checkPasswordResult.innerHTML = &quot;*密码不能为空&quot;;<br><br> obj.focus();<br><br> }else{<br><br> checkPasswordResult.innerHTML = &quot;&quot;;<br><br> }<br><br> }<br><br>

<br><br> function checkrePassword(obj){<br><br> var repassword = obj;<br><br> var checkrePasswordResult = document.getElementById(&quot;checkrePasswordResult&quot;);<br><br> if(repassword.trim().length==0){<br><br> checkrePasswordResult.innerHTML = &quot;*确认密码不能为空&quot;;<br><br> obj.focus();<br><br> }else{<br><br> checkrePasswordResult.innerHTML = &quot;&quot;;<br><br> }<br><br> }<br><br>
</head>

<body>
    <h1>用户注册</h1>
    <form>
    <div>
    账&nbsp;&nbsp;号:<input type="text" name="username"  onblur="checkUserName(this.value);"/>
                        <span  id="checkUserNameResult" style="color: red "></span>
                        <br/><br/>
    密&nbsp;&nbsp;码:<input type="password" name="password" id="pwd1" onblur="checkPassword(this.value);"/>
                        <span id="checkPasswordResult" style="color: red "></span>
                        <br/><br/>
    确认密码:<input type="password" name="repassword" id="pwd2" onblur="checkrePassword(this.value);"/>
                        <span id="checkrePasswordResult" style="color: red "></span>
                        <br/><br/>
    性&nbsp;&nbsp;别:<input type="radio" name="sex" value="man" checked="checked" />男
                        <input type="radio" name="sex" value="woman"  />女
    <br/><br/>
    爱&nbsp;&nbsp;好:<input type="checkbox" name="hobby" value="sport"/>运动
                        <input type="checkbox" name="hobby" value="sport"/>上网
                        <input type="checkbox" name="hobby" value="sport"/>看书
                        <input type="checkbox" name="hobby" value="sport"/>听歌
                        <input type="checkbox" name="hobby" value="sport"/>睡觉
    <br/><br/>
    年&nbsp;&nbsp;龄:<input type="text" name="age" /><br/><br/>
    出生日期:<input type="text"/><br/><br/>
    备&nbsp;&nbsp;注:<textarea name="word" cols=50 rows=10></textarea><br/><br/>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="注册" onclick="return checkpasswd()"/>&nbsp;&nbsp;
    <input type="reset" />
    </div>
    </form>
</body>
</html>

ajax和action如何写啊,才开始学习web,求大神教教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • FiangAsDre GreenRookie 2016-08-01 09:49

    <---form id="from1"><-/form>//form定义一个id
    <--input type="button" value="注册" onclick="checkpasswd()"/->//请求type用button
    function checkpasswd(){
    $("#from1").ajaxSubmit({
    url:ulr,//你的请求地址
    type:"post",//你的请求类型
    dataType:"json",
    data:{},
    success:function(result){
    if(result.status){
    //成功
    alert(resylt.msg);
    }else{
    //失败
    alert(resylt.msg);
    }
    }
    });
    }
    后台action返回方式
    Map map=new HashMap();
    String msg="成功";
    boolean status=true;
    map.put("msg",msg);
    map.put("status",status);
    //map转换成json
    response.getWriter().write(JSONObject.fromObject(map).toString);

    点赞 评论 复制链接分享

相关推荐