如何使用jquery添加href的字符串值?

时间:2011-04-29 17:25:08

标签: jquery input href

我有一个带有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();

4 个答案:

答案 0 :(得分:2)

$('#switch-fighter-search-button-link').attr("href","/fighters/search/" + $('$switch-fighter-text').val());

答案 1 :(得分:0)

试试这个:

$('$switch-fighter-text').keyup(function(){

     $('#switch-fighter-search-button-link').get(0).href = 
                                                 "/fighters/search/"+this.value;

});

这将在搜索框更改

上更新

答案 2 :(得分:0)

抓取两个值并将它们连接起来(用一个?分隔,我在猜测),如下所示:

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

答案 3 :(得分:0)

追加字符串。

//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);
});