使用引导模式(bootbox)进行不引人注目的javascript确认
我的MVC应用程序中有一个链接:使用引导模式(bootbox)进行不引人注目的javascript确认,javascript,jquery,ajax,twitter-bootstrap,unobtrusive-javascript,Javascript,Jquery,Ajax,Twitter Bootstrap,Unobtrusive Javascript,我的MVC应用程序中有一个链接: <a class="btn btn-default" confirm="Are you sure you want to delete the selected group? This action cannot be undone!" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#TabsId-5" href="/Groups/De
<a class="btn btn-default" confirm="Are you sure you want to delete the selected group? This action cannot be undone!" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#TabsId-5" href="/Groups/DeleteGroup/38" id="btnDelGrp"><i class="glyphicon glyphicon-remove"></i> Delete Selected Group</a>
我有一个问题,在对话框显示后,无论发生什么,链接都会触发。低调的ajax似乎不尊重
preventDefault
。我知道有一种解决方法是使用dataajax确认
选项,但它使用通用的js警报弹出窗口。有没有办法让这两件事协同工作?你应该能够破解它,说function=“return false;”“
我会停止使用低调,因为它似乎无法达到它的主要目标:低调 相反,使用。这是一把小提琴的样品: 下面是使锚定功能正常工作的代码片段:
$(function() {
$('body').on('click', 'a.confirm', function(e) {
e.preventDefault();
var $tar = $(e.target);
customConfirm($tar.data('confirm'), function() {
var $contentTarget = $('#' + $tar.data('custom-ajax-target-id'));
$contentTarget.html('Loading...');
$.ajax({
url: $tar[0].href,
// Following two lines for the JSFiddle demo only
type: 'POST',
data: { html: '<h1>' + $tar.html() + '</h1>', delay: 1 }
}).success(function(data) {
$contentTarget.html(data);
});
// You'll want to attach error handlers here to your promise
});
});
});
$(函数(){
$('body')。在('click','a.confirm',函数(e)上{
e、 预防默认值();
var$tar=$(e.target);
customConfirm($tar.data('confirm'),function(){
var$contentTarget=$(“#”+$tar.data('custom-ajax-target-id');
$contentTarget.html('Loading…');
$.ajax({
url:$tar[0]。href,
//以下两行仅用于JSFIDLE演示
键入:“POST”,
数据:{html:'+$tar.html()+'',延迟:1}
}).成功(功能(数据){
$contentTarget.html(数据);
});
//您需要将错误处理程序附加到您的承诺中
});
});
});
以下是示例HTML:
<a href="/echo/html/" data-confirm="Go to Google?" data-custom-ajax-target-id="content" class="confirm">Googs</a><br />
<a href="/echo/html/" data-confirm="Go to Yahoo?" data-custom-ajax-target-id="content" class="confirm">Yahoo</a><br />
如果我在“a”标记上使用非ajax的confirm,那么使用data href不会在任何地方都中断吗?是的,但是你会为它们保留href吗?非ajax和ajax href需要看起来一样有什么原因吗?不,它们没有。。。所以你建议(伪代码)如果数据href存在,就使用它,否则就使用href?我之所以使用它,首先是因为我使用的是一个MVC助手--ActionLink
Ah,现在这更有意义了(助手部分)。老实说,我没有同时尝试过这两种方法,在这种情况下是否可以不使用助手?现在是晚上20:30,我要回家了,但是如果你仍然没有找到答案,我会在早上帮你看一看。是的,我可以按照我们写的那样粘贴链接的HTML,然后扔掉帮助者。我将试一试,看看这是否管用:)受这篇文章的启发,我最终修改了原始代码。下面是我的结论:案例“A”:var url=$(this.attr(“href”);if(!$(this.attr(“ajax replaceTargetId”){action=function(){document.location.href=url;};}else{action=function(){$.ajax({url:url}).done(function(html){$('.#'+$(this.attr(“ajax replaceTargetId”)).html)})}中断;由于某些原因,我无法将其作为代码呈现,抱歉。
<a class="btn btn-default" confirm="Are you sure you want to delete the selected group? This action cannot be undone!" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#TabsId-5" href="#" data-href="/Groups/DeleteGroup/38" id="btnDelGrp"><i class="glyphicon glyphicon-remove"></i> Delete Selected Group</a>
$(document).ready(function ()
{
$("[confirm]").each(function () {
$(this).click(function (e) {
e.preventDefault();
var action = function () { alert("no action"); };
switch ($(this).prop('tagName'))
{
case 'A':
var url = $(this).attr("data-href");
action = function () { document.location.href = url; };
break;
case 'BUTTON':
var form = $(this).closest('form');
if ($(this).attr("name") != "" && $(this).attr("value") != "")
{
var foundIndex = $(form).attr('action').indexOf($(this).attr("name"));
if (foundIndex == -1) //don't add the same param over and over
{
$(form).attr('action', $(form).attr('action') + "?" + $(this).attr("name") + "=" + $(this).attr("value"));
}
else //else replace it
{
$(form).attr('action', $(form).attr('action').substring(0, foundIndex) + $(this).attr("name") + "=" + $(this).attr("value"));
}
}
action = function () { form.submit(); };
break;
}
bootbox.confirm($(this).attr("confirm"), function (result) {
if (result)
action();
});
});
});
});
$(function() {
$('body').on('click', 'a.confirm', function(e) {
e.preventDefault();
var $tar = $(e.target);
customConfirm($tar.data('confirm'), function() {
var $contentTarget = $('#' + $tar.data('custom-ajax-target-id'));
$contentTarget.html('Loading...');
$.ajax({
url: $tar[0].href,
// Following two lines for the JSFiddle demo only
type: 'POST',
data: { html: '<h1>' + $tar.html() + '</h1>', delay: 1 }
}).success(function(data) {
$contentTarget.html(data);
});
// You'll want to attach error handlers here to your promise
});
});
});
<a href="/echo/html/" data-confirm="Go to Google?" data-custom-ajax-target-id="content" class="confirm">Googs</a><br />
<a href="/echo/html/" data-confirm="Go to Yahoo?" data-custom-ajax-target-id="content" class="confirm">Yahoo</a><br />