繁体   English   中英

提交表单时不会显示确认对话框

[英]Confirmation dialog will not show when submitting form

我试图在用户单击删除之前弹出一个确认对话框。

 @using (Html.BeginForm("Delete", "Controller", new { viewModel.Id }, FormMethod.Post, null, new { onsubmit= "return confirm('Do you really want to submit the form?');", @style = "text-align: center" }))
 {
    <input type="submit" value="X" class="form-control btn btn-danger" />
 }

表单似乎按照您的预期呈现:

<form action="/admin/RiskProfile/Delete/24" method="post" onsubmit="return confirm('Do you really want to submit the form?');" style="text-align: center">   
        <input type="submit" value="X" class="form-control btn btn-danger">
        <input name="__RequestVerificationToken" type="hidden" value="CfDJ8O5vcsGUmyZOqz2RSFC3UgK8ICB1W1Ov79zW2-IkboTHIL_LvzQMkjy9s4JsbrA9fEXtE4YfWy1pULXxUk4VhKJc2V53WUuVYJwTB0gbBlxRmM8flrHnFvmtJ8Dr_6zPXmDkZW31Tga5pEiCPw1ebTQMbCLmbhzLKKf9jLErmbajbpUSpYUtZG5H_bMaXw3ptg">
</form>

当我单击按钮时,它只是提交表单而不会弹出确认对话框。

我对 .net 的了解有限,但似乎您只需要防止表单提交事件的默认操作即可。 这里有一些代码可以让你滚动,尽管它可能并不完美:

 @using (Html.BeginForm("Delete", "Controller", new { viewModel.Id }, FormMethod.Post, null, new { onsubmit= "onFormSubmit", @style = "text-align: center" }))
 {
    <input type="submit" value="X" class="form-control btn btn-danger" />
 }
function onFormSubmit (e) {
    e.preventDefault() // The default event action should be ignored
    return confirm('Do you really want to submit the form?');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM