event.preventDefault() 不起作用
我在使用 Telerik Grid 的视图中有这行代码:
columns.Bound(o => o.URI).Width(10).Sortable(false)
.ClientTemplate("<A class='btnGrid' id=source<#= ID #> onclick=GridSelection.addItem('<#= ID #>') >Add</A>").Title("").Width(50);
GridSelection addItem 和disableSelected 函数的 JS 代码:
GridSelection = {
addItem: function (value) {
var anchorOption = $("a[id=source" + value + "]");
anchorOption.click(function (e) { // variable name changed from "event"
e.preventDefault();
return false; // as suggested by mr. Hamdi
});
anchorOption.fadeTo("slow", .5);
GridSelection.disableSelected(anchorOption, true);
var data = $("#GridSource").data('tGrid').data;
var selectedObject;
for (var item in data) {
if (data[item].ID == value) {
selectedObject = data[item];
break;
}
}
var grid = $("#GridSelected").data('tGrid');
var newData = $("#GridSelected").data('tGrid').dataSource._data;
newData.push(selectedObject);
grid.dataBind(newData);
grid.sort("");
anchorOption.fadeTo("slow", .5);
},
disableSelected: function (element, disable) {
//false on IEs 6, 7 and 8
if (!$.support.leadingWhitespace) {
if (disable) {
$(element).attr('disabled', 'disabled');
} else {
$(element).removeAttr('disabled');
}
}
},
// other GridSelection subfunctions here...
当我在 IE 中运行 MVC3 Web 应用程序时,由于 GridSelection.disableSelected 函数,它运行良好,但在 Chrome 和 Mozilla Firefox 中,event.preventDefault();
不起作用。即使用户已经将数据项添加到锚链接中,锚链接仍然会添加数据项。
在被阻止的 GridSelection.addItem
函数中使用 preventDefault
方法是否可以?
preventDefault
阻止了哪个属性,是 href 还是 onclick?
这有什么问题吗? 我该如何修复这个错误? 有谁可以帮忙吗?
i have this line of code in my view using the Telerik Grid:
columns.Bound(o => o.URI).Width(10).Sortable(false)
.ClientTemplate("<A class='btnGrid' id=source<#= ID #> onclick=GridSelection.addItem('<#= ID #>') >Add</A>").Title("").Width(50);
the GridSelection addItem and disableSelected functions' JS codes:
GridSelection = {
addItem: function (value) {
var anchorOption = $("a[id=source" + value + "]");
anchorOption.click(function (e) { // variable name changed from "event"
e.preventDefault();
return false; // as suggested by mr. Hamdi
});
anchorOption.fadeTo("slow", .5);
GridSelection.disableSelected(anchorOption, true);
var data = $("#GridSource").data('tGrid').data;
var selectedObject;
for (var item in data) {
if (data[item].ID == value) {
selectedObject = data[item];
break;
}
}
var grid = $("#GridSelected").data('tGrid');
var newData = $("#GridSelected").data('tGrid').dataSource._data;
newData.push(selectedObject);
grid.dataBind(newData);
grid.sort("");
anchorOption.fadeTo("slow", .5);
},
disableSelected: function (element, disable) {
//false on IEs 6, 7 and 8
if (!$.support.leadingWhitespace) {
if (disable) {
$(element).attr('disabled', 'disabled');
} else {
$(element).removeAttr('disabled');
}
}
},
// other GridSelection subfunctions here...
As I run the MVC3 web app in IE, it runs well because of the GridSelection.disableSelected function, but in Chrome and Mozilla Firefox, the event.preventDefault();
doesn't work. The anchor link still adds the data item even after the user has already added it there.
Is it OK having the preventDefault
method inside the GridSelection.addItem
function that was being prevented?
Which attribute is being prevented by the preventDefault
, is it the href or is it the onclick?
What wrong with this?
How can I fix this bug?
Anyone who can help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的链接的标记有一个内联点击处理程序,但没有
href
:您试图阻止的是
GridSelection.verify()
,您应该注意:e.preventDefault()
都不会不阻止其他处理程序运行,它会停止事件的默认操作,该事件对于链接来说是导航到href
属性中指定的 url。并且您没有指定href
。如果您想在同一元素上为同一事件拥有多个处理程序,则应使用 jQuery 按照您希望调用它们的顺序将它们全部分配,然后使用
event.stopImmediatePropagation()
方法 在其中一个处理程序中(如果您想停止其余处理程序)从跑步。鉴于您没有显示您想要阻止的部分代码,我不知道如何将其放入您所显示的代码中,但一般原则是:
话虽如此,如果
.verify ()
函数调用您的.addItem()
函数,您的目的是阻止未来点击事件在同一链接上工作,因为第一次点击应该验证/add 然后你不想再添加了,然后在您的.addItem()
函数中执行如下操作:The markup for your link has an inline click handler and no
href
:If it is the
GridSelection.verify()
that you're trying to prevent you should note that:e.preventDefault()
doesn't stop other handlers from running, it stops the default action for the event which for a link is to navigate to the url specified in thehref
attribute. And you don't specify anhref
.If you want to have multiple handlers for the same event on the same element you should assign them all with jQuery in the order you want them to be called, and then use the
event.stopImmediatePropagation()
method within one of the handlers if you want to stop the rest of them from running.I don't know how to fit that within the code you've shown given that you don't show part of the code you want to prevent, but the general principle is:
Having said all that, if the
.verify()
function calls your.addItem()
function and it is your intention to prevent future click events from working on the same link because the first click should verify/add and then you don't want to be able to add again, then within your.addItem()
function do something like this:您是否尝试过添加
return false;
而不是event.preventDefault();
Have you tried adding
return false;
instead ofevent.preventDefault();
您是否尝试过重命名事件变量?你可以说我疯了,但我似乎记得遇到问题,只需尝试
function(e) {e.preventDefault();}
看看会发生什么Have you tried renaming the event variable? call me crazy but I seem to recall having issues just try
function(e) {e.preventDefault();}
and see what happens香草(没有 jQuery)的魔力是 stopPropagation。为了阻止任何可能的事件冒泡,您通常需要将两者配对。组合后,您可以实现以下两个目标:
preventDefault
) 和stopPropagation
)。制定最终解决方案:
已更详细地讨论了两者之间的区别这里,以及许多其他地方。
The magic sauce in vanilla (without jQuery) is stopPropagation. In order to stop any possible event bubbling, you usually want to pair the two. When combined, you achieve both:
preventDefault
) andstopPropagation
).Making the final solution:
The difference between the two has been discussed in greater detail here, and many other places.
如果锚点有一个
id
,请使用id选择器而不是将其用作属性选择器。更改
为
我并不是说这是问题所在,但您可以尝试更改此设置。
更新:
event.preventDefault()
阻止浏览器跟踪锚元素的href
属性中设置的链接。它不会阻止或停止click
事件。If the anchor has an
id
use the id selector instead of using it as attribute selector.Change
To
I am not saying this is the issue but you can try changing this.
Update:
event.preventDefault()
stops the browser to follow the link set intohref
attribute of anchor element. It will not prevent or stop theclick
event.