我的HTML.ActionLink中有一个方法,单击该链接后,将打开一个对话框,以确认元素的删除。如果用户单击“是”,则将调用并执行HTML.ActionLink中的方法。否则,该方法不会被调用。但是,尽管在我的javascript函数中放入了event.preventDefault(),但当用户单击“否”时,将调用该方法。以下是我的代码:
Javascript:
function ConfirmerSuppressionPage() {
var x = confirm("Êtes-vous sûr de vouloir supprimer cette page?");
if (x == null) {
event.preventDefault();
return false;
}
if (x == true)
return true;
else
event.preventDefault();
return false;
}
HTML.ActionLink:
<div style="float: left; width: 40px; height: 10px; "> @Html.ActionLink
("-Pg", "SupprimerPage", "Section", new { pageId = @item.Id }, new {
Onclick = "ConfirmerSuppressionPage();", @class = "editLink", style = "width:30px" })</div>
HTML.ActionLink的HTML输出:
<a onclick="ConfirmerSuppressionPage();"
class="editLink ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
href="/Section/SupprimerPage?pageId=70" style="width:30px" role="button" aria-disabled="false">
<span class="ui-button-text">-Pg</span></a>
知道我在这里做错了吗?
参考方案
您必须从以下函数获取事件作为参数:
var els = document.querySelectorAll( 'a' );
for( var i=els.length; i--; ) {
els[i].addEventListener('click', function (event) { //for this a / href
event.preventDefault();
ConfirmerSuppressionPage(); //do whatever you want in here
});
}
function ConfirmerSuppressionPage() {
var x = confirm("Êtes-vous sûr de vouloir supprimer cette page?");
if (x === null) {
return false;
}
if (x === true) {
return true;
} else {
event.preventDefault();
return false;
}
}
https://jsfiddle.net/4tbrs7xv/3/
javascript popupwindow之后的行如何工作? - javascript好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…
Javascript-Urls的奇怪字符串比较行为 - javascript最近,在编写我无法理解的javascript时遇到了字符串比较的问题。我从完全相同的网址创建了两个字符串,当我比较它们时返回false,但是在重新分配相同的字符串后,比较返回true。这是我的示例:var str1 = "http://google.com/"; var str2 = "http://google.com/…
Javascript-从当前网址中删除查询字符串 - javascript单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…
json数组,其中in数组返回错误?坏字符串 - javascript我将json字符串文件解析为python,并且始终返回error。我使用了在线json格式化程序和验证器,它们也返回错误,因此我需要帮助使我的json正确并告诉我错误 [{ "sentence_id": "TR.00001", "sentence": { "text": …
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …