我有些代码有问题。我有一个href链接,一旦单击,它将下载一个文档。出于某种原因,这是对我的jQuery getJSON的干扰。
当我添加e.preventDefault()时,我的代码执行得很完美。当我注释掉e.preventDefault()时,我的JSON代码永远不会执行。
这是我的href链接:
<a href="http://server:88/Documents/@Html.DisplayFor(model => model.path)" data-docid="@Html.Raw(Model.documentID)" class="btn documentLock" data-toggle="modal">
Download Word Version</a>这是我的jQuery:
jQuery('.documentLock').click(function (e) {
// Stop the default behavior
e.preventDefault();
var object = jQuery(this);
var docid = object.attr('data-docid');
jQuery.getJSON("/Document/LockedStatus/" + docid, null, function (data) {
})
.fail('Could not communicate with the server, if this persists please contact the Computer department')
.success(function (data) {
if (data) {
console.log(data);
jAlert('This document is currently locked by and cannot be edited further', 'Document Locked');
}
});
});使用e.preventDefault,我可以启动console.log(数据)。有了e.preventDefault,它就不会开火。因此,我知道代码可以工作,这肯定与下载文档的链接有关。
发布于 2013-09-06 17:13:04
将此添加到代码中
.done(function() {
window.location.replace("your url"); // give your url here
})这是您编辑的代码
jQuery('.documentLock').click(function (e) {
// Stop the default behavior
e.preventDefault();
var object = jQuery(this);
var docid = object.attr('data-docid');
jQuery.getJSON("/Document/LockedStatus/" + docid, null, function (data) {
})
.fail('Could not communicate with the server, if this persists please contact the Computer department')
.success(function (data) {
if (data) {
console.log(data);
jAlert('This document is currently locked by and cannot be edited further', 'Document Locked');
}
})
.done(function () {
window.location.replace("your url"); // give your url here
});
});发布于 2013-09-06 16:38:26
href属性被提升。如果您可以将其更改为javascript:void(0);或者仅仅是一个主题标签,这可能会有所帮助。
你为什么不使用.data()
将此:var docid = object.attr('data-docid');更改为:
var docid = object.data('docid');https://stackoverflow.com/questions/18662487
复制相似问题