[英]How do I add a string value of an href using jquery?
我有一個帶有 href 的輸入按鈕。 我需要在輸入文本框的 href 中的最后一個 / 之后添加一個字符串。 使用 jquery 如何實現這一點? 這是代碼:
//This is the Search Button
$('#switch-fighter-search-button-link').attr("href","/fighters/search/");
//This is the Input box
var sft = $('$switch-fighter-text').val();
$('#switch-fighter-search-button-link').attr("href","/fighters/search/" + $('$switch-fighter-text').val());
嘗試這個:
$('$switch-fighter-text').keyup(function(){
$('#switch-fighter-search-button-link').get(0).href =
"/fighters/search/"+this.value;
});
這將在搜索框更改時更新
抓住這兩個值並將它們連接起來(用?分隔,我猜),如下所示:
var head = $('#switch-fighter-search-button-link').attr("href"); // get existing href
var tail = $('#switch-fighter-text').val(); // get input value
var nhref = head + '?' + tail // join them together, separated by a ? character
$('#switch-fighter-search-button-link').attr('href', nhref); // update the button with the new href value
Append 字符串。
//save the base string somewhere at the beginning of your jquery
var basehref = $('#switch-fighter-search-button-link').attr("href","/fighters/search/");
//add a event handler when the text box is changed to update the button
$('#switch-fighter-text').change(function() {
var sft = basehref + $(this).val();
$('#switch-fighter-search-button-link').attr("href", sft);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.