简体   繁体   English

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

[英]How do I add a string value of an href using jquery?

I have a input button that has an href.我有一个带有 href 的输入按钮。 I need to add a string after the last / in the href from an input text box.我需要在输入文本框的 href 中的最后一个 / 之后添加一个字符串。 How can this get accomplish using jquery?使用 jquery 如何实现这一点? Here is the code:这是代码:

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

Try this:尝试这个:

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

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

});

This will update on the search box change这将在搜索框更改时更新

Grab both values and concatenate them (separated by a?, I'm guessing), like so:抓住这两个值并将它们连接起来(用?分隔,我猜),如下所示:

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 the string. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM