0

所以我是一个 Javascript 新手,在弄清楚如何将变量从我的 HTML 模板传递给 Javascript 时遇到了麻烦。

我想将用户输入的搜索查询传递给 javascript。

这是我获取搜索查询的模板。

<form class="well-whatsnext form-search" style="text-align:center; border-radius:10px 10px 10px 10px; width:900px; height:0px;background-repeat:no-repeat;margin-bottom:100px;"  action="/search/?q="> 
<center>
 <input type="text" id = "form-search" value="Search for Stuff" class="input-xxlarge search-query" style="margin: 30px 0px 0px 70px; height:50px;width:600px;font-size:20px;vertical-align:middle;text-align:center;border-radius:10px 0px 0px 10px;float:left;border-right:0px;color:grey;border-right:0px;" name="q">
</center>
<br>
</form>

这是我的 Javascript 片段:

$("#form-search").click(function() {
    // This sends us an event every time a user clicks the button
    mixpanel.track('MainSearch', {'query' : search-query, 'url' : window.location.pathname});
});

很明显,这search-query不是引用查询的正确方法,因为我没有得到我想要的正确输出。

如何捕获用户搜索的查询变量?

4

2 回答 2

2

用于$('.search-query').val()获取具有该类名的输入元素的值:

{ 'query' : $('.search-query').val() ... }
于 2013-02-12T02:32:05.240 回答
2

尽可能使用纯 JavaScript。

{'query': document.getElementById('form-search').value}
于 2013-02-12T02:36:56.777 回答