設置JQuery事件時繞過窗口上的彈出阻止程序。我想在超鏈接的單擊事件上有條件地顯示一個JQuery對話框。我有一個類似于onCondition1的要求,打開一個JQuery對話,如果條件1不滿足,導航到由其單擊事件的‘href’標記引用的頁面。我能夠在鏈接的點擊事件上調用一個函數。這個函數現在通過執行另一個URL(執行我的Spring控制器并返回響應)來檢查上述條件。所有操作都很完美,只有窗口。打開被彈出窗口阻止。$('a[href*=/viewpage?number]').live('click', function(e) {
e.preventDefault();
redirectionURL = this.href;
pageId= getUrlVars(redirectionURL)["number"];
$.getJSON("redirect/" + pageId, {}, function(status) {
if (status == null) {
alert("Error in verifying the status.");
} else if(!status) {
$("#agreement").dialog("open");
} else {
window.open(redirectionURL);
}
});});如果我把e.preventDefault();從代碼中,popoup阻止程序不會阻塞頁面,但是對于條件1,它會打開對話并打開‘href’頁面。如果我解決了一個問題,就會給另一個人制造問題。我不能同時公正地對待這兩種情況。你能幫我解決這個問題嗎?一旦這個問題解決了,我還有另一個問題要解決,那就是在對話的OK事件上導航:)
3 回答

智慧大石
TA貢獻1946條經驗 獲得超3個贊
window.open
window.open
后來$.getJSON
做點別的事,而不是 window.open
.讓Ajax調用是同步的,這是您通常應該避免的事情,因為它鎖定了瀏覽器的UI。 $.getJSON
相當于: $.ajax({ url: url, dataType: 'json', data: data, success: callback});
.這樣你就可以 $.getJSON
通過將Params映射到上面并添加 async: false
:$.ajax({ url: "redirect/" + pageId, async: false, dataType: "json", data: {}, success: function(status) { if (status == null) { alert("Error in verifying the status."); } else if(!status) { $("#agreement").dialog("open"); } else { window.open(redirectionURL); } }});
同樣,如果您能夠找到實現目標的其他方法,我也不提倡同步Ajax調用。但如果你做不到,那就去吧。 下面是由于異步調用導致測試失敗的代碼示例:
(由于對JSBin的更改,活動鏈接不再工作)
jQuery(function($) { // This version doesn't work, because the window.open is // not during the event processing $("#theButton").click(function(e) { e.preventDefault(); $.getJSON("http://jsbin.com/uriyip", function() { window.open("http://jsbin.com/ubiqev"); }); });});
(由于對JSBin的更改,活動鏈接不再工作)
jQuery(function($) { // This version does work, because the window.open is // during the event processing. But it uses a synchronous // ajax call, locking up the browser UI while the call is // in progress. $("#theButton").click(function(e) { e.preventDefault(); $.ajax({ url: "http://jsbin.com/uriyip", async: false, dataType: "json", success: function() { window.open("http://jsbin.com/ubiqev"); } }); });});

12345678_0001
TA貢獻1802條經驗 獲得超5個贊
$scope.testCode = function () { var newWin = $window.open('', '_blank'); service.testCode().then(function (data) { $scope.testing = true; newWin.location = '/Tests/' + data.url.replace(/["]/g, ""); });};
- 3 回答
- 0 關注
- 524 瀏覽
添加回答
舉報
0/150
提交
取消