我正在尝试在下拉菜单中将Google跟踪代码添加到onChange事件中,但是,当我通过JQuery更改onChange事件时,没有任何帖子/加载。那种已经存在。我只需要从下拉菜单中添加跟踪代码以及所选项目的值。
这是我要添加或更改的onChange事件。这会在页面上对产品进行排序:
Html.DropDownList("products-orderby", Model.AvailableSortOptions, new { onchange = "FiltersPushState(this.value);callAjax(this.value,expiretime.value); return false;" })
以下是我想要添加或更改onChange事件的Google跟踪代码的脚本:
<script type="text/javascript">
$(function () {
$("#products-orderby").each(function () {
var selectedText = $(this).find("option:selected").text();
var selectedValue = $(this).val();
$("#products-orderby").attr("onchange", "FiltersPushState(this.value);callAjax(this.value,expiretime.value); return false; pageTracker._trackEvent('artwork-sort-by', 'sort', products-orderby." + selectedText + ", products-orderby." + selectedValue + ");");
});
$("#products-orderby").change(function () {
var selectedText = $(this).find("option:selected").text();
var selectedValue = $(this).val();
$("#products-orderby").attr("onchange", "FiltersPushState(this.value);callAjax(this.value,expiretime.value); return false; pageTracker._trackEvent('artwork-sort-by', 'sort', products-orderby." + selectedText + ", products-orderby." + selectedValue + ");");
});
});
</script>
任何帮助都会非常感激!
干杯,
〜d
答案 0 :(得分:0)
编辑:
抱歉,我误解了你的问题。您需要取消对pageTracker的调用(我猜这是对谷歌分析的调用,在您的代码中是返回false之后;因此它永远不会执行)
$(function () {
$("#products-orderby").change(function () {
var selectedText = $(this).find("option:selected").text();
var selectedValue = $(this).val();
FiltersPushState(this.value);
callAjax(this.value,expiretime.value);
//pageTracker._trackEvent('artwork-sort-by', 'sort', products-orderby." + selectedText + ", products-orderby." + selectedValue + ");
return false;
});
});
FiltersPushState = function(par1){
alert('FiltersPushState ' + par1);
}
callAjax = function(par1,par2){
alert('callAjax func ' + par1 + ' ' + par2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="expiretime"/>
<select id="products-orderby">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
小提琴:https://jsfiddle.net/HappyiPhone/0k93Lv0g/
根据您的需要进行更改。
$(function () {
$("#products-orderby").change(function () {
var selectedText = $(this).find("option:selected").text();
alert('Selected Text is ' + selectedText);
var selectedValue = $(this).val();
$("#products-orderby").attr("onchange", "FiltersPushState(this.value);callAjax(this.value,expiretime.value); return false; pageTracker._trackEvent('artwork-sort-by', 'sort', products-orderby." + selectedText + ", produc
});
ts-orderby." + selectedValue + ");");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="products-orderby">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>