[英]How to set a custom href into a button when onClick?
我有一個網址
http://localhost:8080/BIM/teacher/reports/section-exercise/assignment?x=1&y=2&z=3
我有2個btns
我不確定如何將這2個btn綁定到正確的URL。
如果單擊了“ Remediation
,則我要將href設置為
http://localhost:8080/BIM/teacher/reports/section-exercise/remediation?x=1&y=2&z=3
唯一的不同是第4段,其余保持不變。
任何幫助/建議,將不勝感激!
我猜-我可能必須:
我希望我在正確的道路上。 讓我知道你們是否注意到一些事情。
$(/*remediation button selector*/).click(function() {
$(/*assignment performance button selector*/).attr('href', 'http://localhost:8080/BIM/teacher/reports/section-exercise/remediation?x=1&y=2&z=3');
});
我不確定您是否要讓“修復”按鈕更改第一個按鈕的href,還是要它們僅具有不同的href? 如果是后者,則它們可以直接在HTML中直接具有不同的href,而無需JS。
您可以在此處將URL更改為所需的URL。 我正在使用Replace fucntion即時更改URL
var url='http://localhost:8080/BIM/teacher/reports/section-exercise/assignment?x=1&y=2&z=3'
$('button1').click(function() {
$(this).attr('href', url);
});
$('button2').click(function() {
$(this).attr('href', url.replace('assignment','remediation');
});
Aminas的回答很好,但是如果第一個URL並不總是相同的話,我建議使用此函數(使用Regex)僅更改URL的文件名:
function changeFileName(strURL, strNewName) {
return strURL.replace(/[^\/?]*(?=\?|$)/, strNewName);
}
這(a)不管原始文件名是什么都有效,並且(b)如果路徑包含文件名,則不會引起問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.