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

发布于 2024-11-03 19:46:21 字数 281 浏览 0 评论 0原文

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

I have a input button that has an href. I need to add a string after the last / in the href from an input text box. How can this get accomplish using 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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放飞的风筝 2024-11-10 19:46:21
$('#switch-fighter-search-button-link').attr("href","/fighters/search/" + $('$switch-fighter-text').val());
$('#switch-fighter-search-button-link').attr("href","/fighters/search/" + $('$switch-fighter-text').val());
狂之美人 2024-11-10 19:46:21

试试这个:

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

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

});

这将在搜索框更改时更新

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

2024-11-10 19:46:21

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

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

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
递刀给你 2024-11-10 19:46:21

附加字符串。

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

Append the string.

//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);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文