Frage

I want to check some condition on blur of input box in jQuery. Once I lose focus from Input box I am not able to get focus again to that box.

Code:

if ($("#Amount").val() < 10000) { 
    alert('The minimum amount is $10,000.'); 
    $("#Amount").focus();
} 
else {
}

Once i lose focus from $("#Amount") it's not setting focus to that control.

Thanks in advance.

War es hilfreich?

Lösung

Well, this is what you are looking for, I believe: http://jsfiddle.net/6Seks/5/

Edit: just noticed how close my answer is to Scroobler's post, though mine doesn't suffer the larger than 10000 bug his does.

Andere Tipps

.focus() in jQuery will focus on the element, but won't give access to the text input when its within a .blur() event. You can use the .setTimeout() function to call the focus so it happens just after the .blur() event:

$("#Amount").blur(function() {
    if ($("#Amount").val() < 10000) { 
        alert('The minimum amount is $10,000.'); 
        setTimeout(function() {
            $("#Amount").focus();
        }, 100);
    }
});

However, I would suggest changing things a little more to check the use has actually input a number as well:

$("#Amount").blur(function() {
    var obj = $(this);
    if (isNaN(obj.val()) || obj.val() < 10000) { 
        $('#result').html('The minimum amount is $10,000.'); 
        setTimeout(function() {
            obj.focus();
        }, 100);
    }
});

See it in action here

*Corrected as per Levi Morrison's comment*

try some thing like this:

$("#Amount").blur(function() {
    if ($(this).val() < 10000) {
        alert('your message...');
        $(this).focus();
    }
})

I´m not sure if i understand what you are looking for.
If you what do disable an input box after doing something you can use:

jquery

$("#Amount").prop("disabled", true);

javascript

document.getElementById("Amount").disabled = true;

so it would be something like:

$("#Amount").change(function() { 
    if (this.value > 10000) {
        alert('The minimum amount is $10,000.');
    }
    else {
        document.getElementById("Amount").disabled = true;
    };
})


...sorry if this isn't what you are looking for.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top