防止data-ajax属性执行

时间:2022-12-05 11:48:59

I have this link (also fiddle):

我有这个链接(也是小提琴):

<a class="update noClick" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#vote11" href="/Tutors/RateNegative/11" onclick="return window.confirm('Are you sure?');">qwer</a>

Now what I want it to do, is, after user clicks on it, I want it to display confirmation window (it does that), but I want to prevent ajax to do anything unless user accepts confirmation window. Currently, if user denies this window, ajax is making the request anyways.

现在我想要它做的是,在用户点击它之后,我希望它显示确认窗口(它会这样做),但我想阻止ajax做任何事情,除非用户接受确认窗口。目前,如果用户拒绝此窗口,则ajax仍会发出请求。

How can I do that?

我怎样才能做到这一点?

2 个解决方案

#1


1  

Try this fiddle: http://jsfiddle.net/YddmF/

试试这个小提琴:http://jsfiddle.net/YddmF/

I just moved the ajax to a separate, hidden anchor, and click it with javascript if the user confirms.

我只是将ajax移动到一个单独的隐藏锚点,如果用户确认,则使用javascript单击它。

HTML

HTML

<a href="#" onclick="window.checkUser();">qwer</a>

<a id="ajax-anchor" class="update" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#vote11" href="/Tutors/RateNegative/11"></a>

JS

JS

window.checkUser = function () {
  if (window.confirm('Are you sure?')) {
        document.getElementById("ajax-anchor").click();
    }
}

CSS

CSS

#ajax-anchor { display: none; }

#2


0  

The solution was pretty simple. Instead of using onclick attribute, I needed to add data-ajax-begin attribute, and put my code in there. If begin returns false, then nothing happens.

解决方案非常简单。我没有使用onclick属性,而是需要添加data-ajax-begin属性,并将我的代码放在那里。如果begin返回false,则没有任何反应。

#1


1  

Try this fiddle: http://jsfiddle.net/YddmF/

试试这个小提琴:http://jsfiddle.net/YddmF/

I just moved the ajax to a separate, hidden anchor, and click it with javascript if the user confirms.

我只是将ajax移动到一个单独的隐藏锚点,如果用户确认,则使用javascript单击它。

HTML

HTML

<a href="#" onclick="window.checkUser();">qwer</a>

<a id="ajax-anchor" class="update" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#vote11" href="/Tutors/RateNegative/11"></a>

JS

JS

window.checkUser = function () {
  if (window.confirm('Are you sure?')) {
        document.getElementById("ajax-anchor").click();
    }
}

CSS

CSS

#ajax-anchor { display: none; }

#2


0  

The solution was pretty simple. Instead of using onclick attribute, I needed to add data-ajax-begin attribute, and put my code in there. If begin returns false, then nothing happens.

解决方案非常简单。我没有使用onclick属性,而是需要添加data-ajax-begin属性,并将我的代码放在那里。如果begin返回false,则没有任何反应。