2013-07-24 182 views
0

我在朋友的幫助下製作了這段代碼。現在我試圖添加如下所述的功能。但我無法取得成功。jquery代碼片段需要小幫助

這裏是小提琴。

小提琴: http://jsfiddle.net/FLgsq/

jQuery代碼:

$(document).ready(function() { 
    $('#word_count').wordCount(); 
}); 

jQuery.fn.wordCount = function (params) { 
    var p = { 
     counterElement: "display_count" 
    }; 
    var total_words; 

    if (params) { 
     jQuery.extend(p, params); 
    } 

    //for each keypress function on text areas 
    this.keypress(function() { 
     total_words = this.value.split(/[\s\.\?]+/).length; 
     jQuery('#' + p.counterElement).html(total_words); 
    }); 
}; 

$(document).ready(function() { 
    $('input[name=rdOptin]').click(function() { 
     var selected = $(this).val(); 
     var var0 = 0.3; 
     var var1 = 20; 
     var var2 = var1 * 3.9/100; 
     var var3 = 20; 
     var var4 = (var1 + var3) * 3.9/100; 
     if (selected == 'norm') 
      var total = var0 + var1 + var2; 
     else 
      var total = var0 + var1 + var3 + var4; 
     $("#calc_value").html("<span style='color: red;'>Numbers</span> <span style='color: green;'>" + total.toFixed(2) + "</span>"); 
    }); 
}); 

我試圖做到的,是的,我該怎麼辦,如果詞在文本框(其數量已經由計函數「jQuery.fn.wordCount」)增加超過13,變量「total」的輸出相應地隨着已經存在的函數發生變化(即,它也在監視無線電「rdOptin」並基於該函數進行計算)。相反,它只是加了+5,或者它的數字在13-18之間,它應該將5加到部分「Calculated Value:」的輸出,然後如果18-23加10,那麼24-28加15等等或6次迭代可以採取。

怎麼能做到男人?

+0

看到這裏http://stackoverflow.com/questions/7422192/jquery-count-words-in-real-time – maximkou

回答

1
$(document).ready(function() { 
    $('input[name=rdOptin]').click(function() { 
     var selected = $(this).val(); 
     var var0 = 0.3; 
     var var1 = 20; 
     var var2 = var1 * 3.9/100; 
     var var3 = 20; 
     var var4 = (var1 + var3) * 3.9/100; 
     if (selected == 'norm') 
      var total = var0 + var1 + var2; 
     else 
      var total = var0 + var1 + var3 + var4; 
     var wordcount = parseInt($("#display_count").html(), 10); 
     if (wordcount >= 13) { 
      var inc = Math.ceil((wordcount-12)/5); 
      total += inc; 
     } 
     $("#calc_value").html("<span style='color: red;'>Numbers</span> <span style='color: green;'>" + total.toFixed(2) + "</span>"); 
    }); 
}); 

FIDDLE