【问题标题】:jQuery find and replace text in a variablejQuery 查找和替换变量中的文本
【发布时间】:2011-03-05 10:08:04
【问题描述】:

我有一个类似的变量

var theUrl = 'http://www.google.com/?q=%s';

然后我有一个像这样的输入框

<input type="text" id="searchBox" value="" />

还有一个按钮

<input type="button" id="searchButton" value="Search" />

当按钮被点击时,我应该得到一个警报,其中 var theUrl%s 应该替换为用户在文本框中输入的文本

如何做到这一点?我认为 find() 函数只替换 html 元素!

请帮忙

【问题讨论】:

    标签: jquery html replace find


    【解决方案1】:

    你需要调用replace函数,像这样:

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

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 2020-08-23
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多