简体   繁体   中英

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

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();
$('#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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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