layui的以ajax提交表单不成功问题?

大哥大姐们哪位知道,layui的以ajax提交表单不成功问题??

<body>
<div class="layui-form-item">
<form id="myForm" class="layui-form" >
<div class="layui-form-item" >
<label class="layui-form-label" style="padding-top: 10px ; padding-left: 30px;padding-right:40px">周一</label>

<div class="layui-form-mid layui-word-aux" style="margin-left:30px"> <input type="week" name="week_1" required lay-verify="required" placeholder="周数" autocomplete="off" class="layui-input" ></div>
<div class="layui-form-mid layui-word-aux" style="margin-left:20px" > <input type="text" name="resume_1" required lay-verify="required" placeholder="工作简述" autocomplete="off" class="layui-input" ></div>
<div class="layui-form-mid layui-word-aux" style="margin-left:20px ;width:200px"> <textarea name="details_1" required lay-verify="required" placeholder="请输入工作详情:" class="layui-textarea" style="height:70px ;margin-top:-12px" ></textarea> </div>

<div class="layui-form-item" style="margin-left:500px">
<div class="layui-input-block" >

<!-- lay-submit ="" lay-filter="formDemo onclick="submit()" -->
<button class="layui-btn" lay-submit lay-filter="formDemo" type="submit" >立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary" style="float:right;margin-right:800px">重置</button>
</div>
</div>
</div>

</form>
<div class="layui-inputv-block layui-text" id="error"></div>

<br><br>

<script src="layui.js" type="text/javascript"></script>

<script>
function submit(){
alert("1111");
$.post("write_weekly", $(".layui-form").serialize(), function (data) {
alert(222);
if (data.flag) {
$("#error").html(data.errorMsg);
}
else {
$("#error").html(data.errorMsg);
}
});
}
</script>

<script>
//Demo
layui.use('form', function(){
var form = layui.form;
var $ = layui.jquery;
//监听提交
form.on('submit(formDemo)', function(data){
/* layer.msg("提交之前"); */
$.ajax({
type: 'post',
url: '/servlet/write_weeklyServlet',
dataType:'json',
data: data.field,
success: function (res) {
layer.msg("提交成功");
if (res.code==0) {

layer.close(layer.index);
window.parent.location.reload();
} else {

layer.alert(res.msg, {icon: 5}, function () {

});
}
},error:function (result) {
layer.msg("提交失败");
}
});
/* 显示输入的信息*/
layer.msg(JSON.stringify(data.field));
return false;
});
});
</script>
</body>
</html>
大佬们,一直提交失败,怎么解决?
有什么可能元原因呢?
最新回答
最纯洁De天使つ

2024-07-28 09:55:47

首先submit()这个函数没有调用先删掉,然后form表单标签加action="javascript:void(0);" onsubmit="return false;" 然后再提交方法里面看能不能打印回调的那个res; 如果报错肯定是后端返回的报错。