ASP.NET MVC 中的 JavaScript 确认表单

Javascript confirm form in ASP.NET MVC

本文关键字:确认 表单 JavaScript 中的 NET MVC ASP      更新时间:2023-09-26

我的代码有什么问题?这是确认对话框。如果用户单击"是",他/她必须导航到工作控制器的"索引"视图。如果用户单击否,他/她必须导航到产品控制器的索引视图。但是导航不起作用!

 <input type="button"  onclick="if (confirm('Format the hard disk?'))
$.ajax({
    url: '/Work/Index/24',
    data: { ID: myJSVariable },
    type: 'get',
    traditional: true
});
else {
$.ajax({
    url: '/Product/Index/24',
    traditional: true
})
}" />

问题出在哪里?还有其他建议吗?

如果要导航,则需要使用window.location = "/Work/Index/24"

如果要使用 $.ajax 进行异步调用,则需要添加成功函数。

$.ajax({
    ...
    success : funcion(result){
        alert('ajax completed successfully');
    }
});