2013-12-07 2 views
0

Мне нужно отобразить html простой математической подстановки.Простая математическая подстановка (jquery)

treshold - count = currentCount 

Ниже мой текущий сценарий - он работает правильно, чтобы скрыть/показать DIV. Но мне нужно, чтобы изменить код, чтобы он также будет отображать currentCount в HTML

var threshold = 100; // Number to trigger on 

var widgets_to_hide = ["#promotion"]; // CSS selectors of widgets to hide when triggered 

var widgets_to_show = ["#text"]; // CSS selectors of widgets to show when triggered 



$(function(){ 
$('.ss_entry_count').on('changed.content', function(event){ 
var count = parseInt($('.ss_entry_count_value', this).text()); 


if(count - threshold){ 


$.each(widgets_to_hide, function(i){ 
$(widgets_to_hide[i]).hide(); 
}); 


$.each(widgets_to_show, function(i){ 
$(widgets_to_show[i]).show(); 
}); 


} 
}); 
}); 


<span id="result"></span> 

ответ

1

Вам нужно назначить treshold - count к currentCount, а затем установить текст #result. Кроме того, не забудьте добавить параметр базисное к parseInt:

$(function(){ 
    $('.ss_entry_count').on('changed.content', function(event){ 
     var count = parseInt($('.ss_entry_count_value', this).text(), 10); 
     var currentCount = treshold - count; 
     if(currentCount){ 
      $.each(widgets_to_hide, function(i, widget){ 
       $(widget).hide(); 
      }); 
      $.each(widgets_to_show, function(i, widget){ 
       $(widget).show(); 
      }); 
     } 

     $("#result").text(currentCount); 
    }); 
}); 

Вы могли бы упростить еще больше:

// use a single selector to specify which widgets to show/hide 
var widgets_to_hide = "#promotion"; 
var widgets_to_show = "#text"; 

$(function(){ 
    $('.ss_entry_count').on('changed.content', function(event){ 
     var count = parseInt($('.ss_entry_count_value', this).text(), 10); 
     var currentCount = treshold - count; 
     if(currentCount){ 
      $(widgets_to_hide).hide(); 
      $(widgets_to_show).show(); 
     } 

     $("#result").text(currentCount); 
    }); 
}); 
+0

p.s.w.g - Вы спасли день: D – aksupersurfer

Смежные вопросы