当我按下在视图中创建项目时,我要做的是弹出确认对话框。从我通过阅读其他帖子(如果我错了,纠正我)的理解是通过使用jquery。我对jquery / javascript不是很熟悉所以我正在尽我所能去理解我在做什么。我在网上找到的代码就是这个。
asDate()
现在如何按下按钮,它立即在我的控制器中激活我的POST创建方法而不显示对话框。有人可以解释为什么会发生这种情况以及我如何解决它。我已经尝试添加编写asTime()
的代码,但我真的不知道我在寻找什么或者需要在那里写什么。
我已阅读的帖子
Where i got the above code from
答案 0 :(得分:2)
一种方法是使用<input type="button" />
。然后为表单调用submit()
。
<form method="post" id="sampleform">
<input id="Create" name="Common" type="button" value="Create" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#Create").click(function (e) {
if (confirm("Are you sure?")) {
console.log('Form is submitting');
$("#sampleform").submit();
} else {
console.log('User clicked no.');
}
});
});
</script>
</form>
如果您使用ASP.NET MVC,您可能需要考虑使用Html.BeginForm Html Helper。
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { Id = "sampleform"}))
{
<input id="Create" name="Common" type="button" value="Create" />
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<script type="text/javascript">
... Same as above
</script>
答案 1 :(得分:0)
尝试添加onclick
<form method="post">
<input id="Create" name="Common" type="submit" value="Create" onclick="DeleteFunction()"/>
...
</form>
<script>
function DeleteFunction()
{
if(confirm('Are you sure?'))
return true;
} else {
return false;
}
}
</script>
答案 2 :(得分:0)
你所拥有的应该工作得很好。我使用on方法但click方法应该具有相同的效果。你不需要指定window.confirm,只是确认也适用于此。
$("#Create").on("click", function (e) {
// use whatever confirm box you want here
if (!confirm("Are you sure?")) {
e.preventDefault();
}
});
确保:
$("#Create").length
如果它没有返回1,那就是问题