2013-05-14 112 views
1

我在電子商務中有一個購物車,並且當購物車的值改變橫幅出現對於每個值不同。將事件偵聽器添加到HTML span標記

當頁面重新加載時(當客戶在購物車中添加其他產品時發生這種情況),js將運行並顯示正確的橫幅。

但是,當客戶端只更改產品數量或從購物車頁面中刪除產品時,不會重新加載那個使用事件偵聽器的人。

除了我的問題是值($)是在標籤<span> $ value </ span>內而不是在輸入內,所以我不能使事件監聽器功能。

我該如何運用?代碼如下:

jQuery(document).ready(function(){ 
setTimeout(function(){ 
    trocaBannerCarrinho(); 
}, 5000); 

//here is the event listener that does not work!! 
jQuery("#value_total").on("change", function(){ 
    trocaBannerCarrinho(); 
}); 
}); 


function trocaBannerCarrinho(){ 

// condições para exibir os banners 
var valor_cond1 = {min: 1, max: 980};//banner 1 
var valor_cond2 = {min: 981, max: 1280};//banner 2 
var valor_cond3 = {min: 1281, max: 1580};//banner 3 
var valor_cond4 = {min: 1581, max: 1980};//banner 4 
var valor_cond5 = {min: 1981};//banner 5 

// recuperando o valor do carrinho 
var total_carrinho = jQuery("#value_total").html(); 

// removendo R$, o ponto e trocando a virgula por um ponto 
// o resultado final fica assim: 35.00, 1900.99, 20000.58, etc... 
total_carrinho = total_carrinho.replace(/\s/g,'').replace('R$','').replace('.','').replace(',','.'); 

if ((total_carrinho >= valor_cond1.min) && (total_carrinho <= valor_cond1.max)) { 
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner1').show(); 

} else if ((total_carrinho >= valor_cond2.min) && (total_carrinho <= valor_cond2.max)) { 
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner2').show(); 

} else if ((total_carrinho >= valor_cond3.min) && (total_carrinho <= valor_cond3.max)) { 
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner3').show(); 

} else if ((total_carrinho >= valor_cond4.min) && (total_carrinho <= valor_cond4.max)) { 
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner4').show(); 

} else if (total_carrinho >= valor_cond5.min) { 
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner5').show(); 
} 
} 
+0

'value_total'是如何定義的? – karthikr

回答

2

我的猜測是,聽衆沒有得到更新。試試做

jQuery("document").on("change", '#value_total' ,function(){ 
    trocaBannerCarrinho(); 
}); 
相關問題