jQuery查找并替换变量中的文本

Aak*_*thy 2 html jquery replace find

我有一个像变量

var theUrl = 'http://www.google.com/?q=%s';
Run Code Online (Sandbox Code Playgroud)

那我就像一个输入框

<input type="text" id="searchBox" value="" />
Run Code Online (Sandbox Code Playgroud)

还有一个按钮

<input type="button" id="searchButton" value="Search" />
Run Code Online (Sandbox Code Playgroud)

单击按钮时,我应该收到一个警告,其中var theUrl%s应该替换为用户在文本框中输入的文本

这该怎么做 ?我认为find()函数只替换html元素!

请帮忙

SLa*_*aks 7

你需要调用这个replace函数,如下所示:

$('#searchButton').click(function() { 
    alert(theUrl.replace('%s', $('#searchBox').val());
});
Run Code Online (Sandbox Code Playgroud)