form表单提交,用submit方法并且跳到指定页面或不跳转

来源:互联网 发布:深圳美生创谷淘宝地址 编辑:程序博客网 时间:2024/05/18 02:37

开发环境后台Java

后台根据提交的整个表单,通过name名对应的value值;

<form method="POST" class="form-horizontal" name="myform" action='http://192.168.20.205:8080/platform/project/enroll' id="formde" target="" enctype="multipart/form-data" onsubmit="return formText()"><p id="clickperson">项目申请表</p><div id="personalshow"><p><span>负责人姓名<b>*</b></span><input type="text" required name="project.user_name" id="user_name" value="" style="background: url(images/ren.png) no-repeat center left;" /></p><p><span>性别</span><div id="aaas"><div id="onswitch">  女   <input type="checkbox" class="onswitchs sexstyle" />    男</div></div></p><p><span>联系邮箱<b>*</b></span><input type="text" required name="project.email" id="emiasdz" value="" style="background: url(images/emio.png) no-repeat center left;" /></p><p><span>微信号<b>*</b></span><input type="text" name="project.wechat" id="wxname" value="" style="background: url(images/wx.png) no-repeat center left;" /></p><p><span>联系电话<b>*</b></span><input type="text" required name="project.phone" id="phonss" value="" style="background: url(images/tpname.png) no-repeat center left;" /></p><p><span>项目名称<b>*</b></span><input type="text" required name="project.name" id="xmname" value="" style="background: url(images/xmname.png) no-repeat center left;" /></p><p id="filestyle"><span>上传BP</span><span style="width: 73%;position:absolute;left:25%;top:0;height: 40px;" id="filetext">点击此处上传文件</span><input type="file" name="project.file_url" id="bpclick" value="" /></p><p><span>项目简介<b>*</b></span><textarea type="" id="" name="project.project_description" /></textarea></p></div><!--<input type="button" value="" class="btn2" onclick = "checkUser();" />--><input type="submit" name="projectform" value="提交" id="submits" style="width: 150px;" /></form>

请大家不要在意细节委屈

在网上看了很多方法感觉不稳妥最后自己都不知道什么情况了;但是有一点我引入了jq的form.js;

<script src="js/jquery-form.js"></script>

                  $("#filetext").text(name)// console.log();})function formText() {

    $("#formde").ajaxSubmit(function(message) {

window.location.href = "index.html";})

$.ajax({

     url: "你的url",

type: "post",

dataType: "jsonp",

jsonp: "callback",

     async: false,success: function(data) {},

error: function(data) {

      if(data.status == 200) {var result = document.getElementById("user_name").value;

       var emiasdz = document.getElementById("emiasdz").value;

       var phonss = document.getElementById("phonss").value;

var xmname = document.getElementById("xmname").value;

if(result == "") {

alert("请填写您的中文姓名");

return false;}

if(emiasdz == "") {

        alert("请填写邮箱");

return false;}if(xmname == "") {

alert("请填写项目名称");

return false;}

if(!(/^1[3|4|5|8][0-9]\d{8}$/.test(phonss))) {

alert("请填写正确手机号码");

return false;

}alert("资料上传成功");

window.location.href = "index.html";

      } else {

       alert("发送过程出现问题,请检查网络并重新提交!");}}});

return false;}

我最后的时候投机取巧了,ajax返回直接进入到错误中但是还是200 4所以我直接判断是从错误中获取的,后台也接收到数据,所以我才大胆的这么做了。

window.location.href = "index.html"; 是用来连接新的页面,这个都是知道的。

这是他的基础代码
利用jquery的ajaxSubmit函数以及form的onsubmit函数完成,如下: 

复制代码代码如下:


< form id="saveReportForm" action="saveReport.htm" method="post" onsubmit="return saveReport();"> 
< input type="submit" value="保存报告"/> 
< /form> 


form增加一个id用于在jquery中调用,增加一个onsubmit函数用于submit前自己提交表单 

saveReport对应函数为 

复制代码代码如下:

function saveReport() { 
// jquery 表单提交 
$("#saveReportForm").ajaxSubmit(function(message) { 
// 对于表单提交成功后处理,message为提交页面saveReport.htm的返回内容 
}); 

return false; // 必须返回false,否则表单会自己再做一次提交操作,并且页面跳转 

希望大家能给出更好的建议和意见相互学习



阅读全文
0 0
原创粉丝点击