使用Javascript函数防止MVC表单提交
Prevent MVC form submit using Javascript Function
我有一个upload.ascx
在我的项目。它将被加载到Jquery Popup中。
upload.ascx
包含File Upload
控制。文件上传控件将上传.xlsx and .xls
文件。我已经添加了Java script
函数来做这个验证(防止不必要的文件上传)。
Onchange of FileUpload
控件和Onclick of Submit button
将调用相同的Validation函数。
文件上传验证函数对两个调用都有效。如果用户单击提交按钮,则调用相同的函数并正常工作。
我的问题是:在我的验证函数我已经写了return false
。在显示警报消息之后,页面被重定向到某个URL (localhost/Admin/Authorization/AcceptFile.aspx
)。AcceptFile
是我的ActionResult名称。这不应该发生。函数返回False。但是表单被重定向到上面的URL为什么?
我一直保持调试器在我的动作结果,没有得到调用,如果它的错误的文件(它的正确)。如果它是正确的文件,索引文件将加载成功消息(操作结果正在被调用并且工作正常).
我怀疑MVC表单。如果上传了错误的文件,重定向正在发生,而没有调用操作结果,则应停止此操作。
<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
我的Javascript函数:
function checkfile(sender) {
var validExts = new Array(".xlsx", ".xls");
var fileExt = sender.value;
var strValue = false;
fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
if (validExts.indexOf(fileExt) < 0) {
ShowMessage(-1, "Invalid file selected, valid files are .xlsx and .xls types.");
strValue = false;
}
else {
strValue = true;
}
return strValue;
}
我的上传。ascx代码:
<div>
<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<input type="file" id="fileAuthorization" name="fileAuthorization" onchange="checkfile(this);" />
<input type="submit" id="btnSave" name="btnSave" value="Upload" onclick="javascript:return checkfile(document.getElementById('fileAuthorization'))" />
<%} %>
</div>
我发现问题了
是我的怀疑是正确的。问题是
onsubmit = "javascript:return checkfile(document.getElementById('fileAuthorization'))"
this应该在Form标签中命名为Like this:
<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "javascript:return checkfile(document.getElementById('fileAuthorization'))" }))
{%>
之前叫做Submit button Onclick
<input type="submit" id="btnSave" name="btnSave" value="Upload" onclick="javascript:return checkfile(document.getElementById('fileAuthorization'))" />
我已经纠正了,它正在工作(验证和文件上传提交按钮点击和文件上传控制更改)。
但我的问题是为什么return false
不工作提交按钮点击?但它的工作为Fileupload onchange事件
重定向发生的原因。在将同一行更改为MVC表单后,它开始工作(重定向现在没有发生)为什么?
将提交更改为:
<button type="button" id="btnSave" name="btnSave" onclick="checkfile(document.getElementById('fileAuthorization'))" ></button>
它不会自动提交表单
当你准备好提交表单时,你可以在javascript的回调函数中这样做:
document.getElementById('form-id').submit();
- ASP MVC复选框表单提交问题
- jquery禁用表单提交时输入MVC 4
- 客户表单提交日期时间,MVC应用程序
- 在表单提交后,无法使用 mvc razor 中的 ajax 从其中的另一个分部视图刷新分部视图
- MVC/Javascript:如何在表单提交中发布整数数组
- ASP MVC jQuery验证专注于工作,但不专注于表单提交
- 表单提交后显示感谢信息mvc umbraco
- Spring MVC-处理错误,使用多个表单提交表单
- 表单提交和javascript函数调用提交按钮点击在spring mvc
- ASP.NET MVC表单:可以用ENTER键提交,但不能用submit按钮提交
- Jquery表单提交不会到达MVC控制器
- 使用Javascript函数防止MVC表单提交
- 没有表单提交的MVC表单验证
- 在ASP中.NET MVC 3视图,如何在JavaScript中处理所有表单提交
- 取消MVC表单提交
- 在表单提交后启用文本框和按钮.Net MVC 4
- 表单提交后的ASP.net MVC成功提示
- 防止表单提交,使用Ajax检查记录,如果有记录,请继续ASP.NET MVC
- 数据表在表单提交时未将 rowid 返回给 asp.net MVC 控制器
- 不会提交带有名称的 MVC 表单输入按钮