1

I am not a "Programmer" I am a html/css hack but I am trying to write this little jquery widget. This is not the best format for this question here, I know that, I am open to edits and reworking to make it a SO quality question. The widget jsfiddle

$(document).ready(function () {
function titleInc() {
    var length = $('#title').val().length;
    if (length >= 5 && length <= 10) {
        return true;
        // IF it already returned true, don't return true again
    } else {
        return false;
    }
}

$('#title').blur(function () {
    var current = parseInt($('#end_val').val(), 10);
    if (titleInc) {
        $('#end_val').val(current + 12);
    }
});
});

Basically its a score keeping tool. The challenge is that if the function keeps returning true then the person gets to many points. The function is checking the char value of a input. if its between a two values the person should get the points. but they shouldn't get more if the go back to the input and retab out.

IF onchange is below/above false if onchange is in range true if onchange still is ok null

any ideas?

4

3 回答 3

0

Try this

   $(document).ready(function () {

    var check = false;
    function titleInc() {
        var length = $('#title').val().length;
        if (length >= 20 && length <= 120 && !check)  {
            check = true;
            return true;
        } 
        return false;
    }

    $('#content-title-field').blur(function () {
        var current = parseInt($('.dial').val(), 10);
        if (titleInc()) {
            $('.end_val').val(current + 12);
        }
    });



});
于 2013-05-13T04:32:23.633 回答
0

Use some kind of flag that will be set once function returns true.

var trueReturnFlag = false;

function titleInc() {
    var length = $('#title').val().length;
    if (length >= 5 && length <= 10 && !trueReturnFlag) {
        trueReturnFlag = true;
        return true;
        // IF it already returned true, don't return true again
    } else {
        return false;
    }
}

and change condition to --

  if (titleInc()) {
于 2013-05-13T04:35:02.903 回答
0

http://jsfiddle.net/jNtJA/5/

HTML:

Dial<input class="dial" value="10"/>+
<br>
Title<input id="title" />=
<br><br>   
(must be bigger that 19 and lower than 121)
<br><br>
Dial + 12 = <input id="end_val"/>
(result will show up on blur if titleInt() function is frue)

jQuery:

function titleInc(){
   var length = parseInt( $('#title').val(), 10 );
   return (length >= 20 && length <= 120);
}

$('#title').blur(function(){
    var current = parseInt( $('.dial').val(), 10 );
    if( titleInc() ) $('#end_val').val(current + 12);                                   
});
于 2013-05-13T04:43:56.150 回答