我有以下用于 onclick 代碼的 jquery:
function find(){
$.ajax({
type: "post",
url: link 'register/data',
data: {},
success: function () {
window.location.href= "mycontroller/myfunction"
}
});
}
我想讓 url 每次單擊按鈕時都保留在 mycontroller/myfunction 上,我嘗試了子字串 lastindexof 但每次單擊按鈕時它都會添加更多 url,我如何讓按鈕在單擊時保持不變?提前致謝
uj5u.com熱心網友回復:
讓我們假裝你在http://example.com。您將頁面重定向到"test",所以現在您在http://example.com/test。現在你再次重定向到"test"。哦哦,現在你開始了http://example.com/test/test。不想發生這種事,是嗎?你再重定向一次到"other/test". 哦不,現在你開始了http://example.com/test/test/other/test!
解決這個問題的方法在于單個位元組。更改test為/test,因此它將始終與基本 URL 而不是當前 URL 進行比較。
在您的 JavaScript 中:
window.location.href = "/mycontroller/myfunction";
無論你點擊它,你都會一直在http://example.com/mycontroller/myfunction。
uj5u.com熱心網友回復:
您可以將成功功能更改為這樣使用window.location.origin:
function find(){
$.ajax({
type: "post",
url: link 'register/data',
data: {},
success: function () {
window.location.href = window.location.origin "/mycontroller/myfunction"
}
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439713.html
標籤:javascript jQuery
