gpt4 book ai didi

javascript - Ajax.BeginForm OnBegin 确认 Via jquery modal

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:29 24 4
gpt4 key购买 nike

我正在使用 jQuery UI 对话框。我有一个删除表单如下:

@using (Ajax.BeginForm("DeleteUser", "Administrator", new { id = Model }, new AjaxOptions { OnSuccess = "Deleted", OnBegin = "DeletingUser" }, new { id = "frm" + Model, name = Model }))
{
<input type="submit" value="" />
}

我希望在发送 ajax 请求之前,弹出一个模式确认,用户选择是或否。

这是我的javascript:

<script>
function DeletingUser(){
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
//I need to return a true or false here depending on the button clicked.
}
</script>



<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

如在 javascript 代码中所见,对话框是异步打开的,这导致该方法不返回任何内容,并且无论如何都会提交表单,而无需用户选择是或否。我该如何解决这个问题?

最佳答案

您可以使用普通的 Html.BeginForm 并使用 jquery 对其进行 AJAX 化。与 Ajax.BeginForm 助手相比,您将拥有更多的控制权:

@using (Html.BeginForm(
"DeleteUser",
"Administrator",
new { id = Model },
FormMethod.Post,
new { id = "frm" + Model, name = Model }
))
{
<input type="submit" value="" />
}

然后在一个单独的 javascript 文件中简单地:

$(function() {
$('form[id^="frm"]').submit(function() {
var $form = $(this);
$('#dialog-confirm').dialog({
resizable: false,
height:140,
modal: true,
buttons: {
'Delete all items': function() {
$(this).dialog('close');
// the user confirmed => we send an AJAX request to delete
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
data: $form.serialize(),
success: function(result) {
Deleted(result);
}
});
},
Cancel: function() {
$(this).dialog('close');
}
}
}
return false;
});
});

关于javascript - Ajax.BeginForm OnBegin 确认 Via jquery modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com