2

我有一个正在处理的基本积分计数器小部件。我有一个较早的问题得到了回答,以帮助我开始行动。但我有最后一个问题。基本上,我有一个返回真或假的函数,但我需要第三个选项。

逻辑是:

  • 如果输入字符数在范围内,则用户获得积分
  • 如果用户编辑和计数保持在范围内,则没有新点
  • 如果用户在范围内编辑和计数超出范围,则用户失去积分

我想我有这个权利,只是不确定如何完成该功能。

http://jsfiddle.net/jNtJA/8/

 var check = false;

 function titleInc() {
     var length = $('#title').val().length;
     if (length >= 5 && length <= 10 && !check) {
         check = true;
         return true;
     } else if (check  && length < 5 && length > 10) {
         // Set the function to return DECREMENT
     } else {
         return false;
     }

     $('#title').blur(function () {
         var current = parseInt($('#end_val').val(), 10);
         if (titleInc()) {
             $('#end_val').val(current + 12);
         } else if(  ){
         // THERE NEEDS TO BE A WAY TO DECREMENT
         }
     });

 });
4

1 回答 1

3
var options = {
         increment: 1,
         decrement: 2, 
         doNothing: 3
     };

 function titleInc() {
     var length = $('#title').val().length;
     if (length >= 5 && length <= 10 && !check) {
         check = true;
         return options.increment;
     } else if (check  && length < 5 && length > 10) {
         return options.decrement;
     } else {
         return options.doNothing;
     }

     $('#title').blur(function () {
         var current = parseInt($('#end_val').val(), 10),
             option = titleInc();
         if (option === options.increment) {
             $('#end_val').val(current + 12);
         } else if(option === options.decrement){
             $('#end_val').val(current - 12);
         }
     });
于 2013-05-13T19:28:06.293 回答