[英]Javascript and HTML - Variable Doesn't Show Up
I have the following code snippet in HTML: 我在HTML中有以下代码片段:
<form class="navbar-search pull-right search-query" value = "" action="/search/?q=" style="line-height:25px;">
<input type="text" id="navbarsearch" class="search-query span2" style="border-radius:14px 14px 14px 14px;" name = "q" placeholder="Search">
<i class="icon-search icon-white" onclick="document.forms.search.submit();"></i>
</form>
I'm trying to pass this variable to Javascript as follows, but the query variable is not being passed properly for some reason I don't think.... 我正尝试按以下方式将此变量传递给Javascript,但由于某些我不认为的原因,查询变量未正确传递。
<script type="text/javascript">
$("#navbarsearch").click(function() {
// This sends us an event every time a user clicks the button
mixpanel.track('SearchQuery', {'query': document.getElementById('navbarsearch').value, 'url' : window.location.pathname});
});
</script>
The action is showing up in MixPanel, but the query variable is not. 该操作显示在MixPanel中,但查询变量未显示。 Am I doing something wrong? 难道我做错了什么?
You need to put it inside the document.ready: 您需要将其放在document.ready中:
$(function() { // <--- there.
$("#navbarsearch").click(function() {
mixpanel.track(
'SearchQuery',
{
'query': $(this).val(),
'url' : window.location.pathname
}
);
});
});
The click event is set off when a user clicks with their mouse. 当用户用鼠标单击时,单击事件被关闭。 I think you want to listen to keystrokes? 我想您想听击键吗?
Try: 尝试:
$("#navbarsearch").keydown(function() {
mixpanel.track('SearchQuery', {
'query': document.getElementById('navbarsearch').value,
'url' : window.location.pathname
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.