-2

Is there a clear cut method to changing the contents of a url in href based on user submitted information?

For EXAMPLE: A user submits the word "basketball" into a search box and the result is the following link.

<a href="http://google.com/basketball>More information</a>

If the user submitted lets say "football" the link would then change to:

<a href="http://google.com/football>More information</a>
4

2 回答 2

2

您缺少 href 的关闭引号,您可以这样做。

现场演示

var url = $('#aId').attr('href');
firstPart = url.substring(0, url.lastIndexOf('\/')+1);
$('#aId').attr('href', firstPart + "Football");
于 2012-11-16T05:29:01.250 回答
1

鉴于: <a id="url" href="http://google.com/basketball">More information</a> <input id="sport" type="text" val="soccer" />

var $val = $('#sport').val()
$("#url").attr("href", "http://www.google.com/" + $val);
于 2012-11-16T05:30:12.157 回答