最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I call this function on blur(), using JQuery? - Stack Overflow

programmeradmin1浏览0评论

I have this function, see below:

function checkStartPrice (){
    if ($('#StartingPrice')[0].value.length == 0){
        alert("The 'Starting Price' cannot be left empty!");
        return false;
     } else {
        var BuyItNowPrice = parseFloat($('#BuyItNowPrice').val());
        var StartingPrice = parseFloat($('#StartingPrice').val());
        var Reserve = parseFloat($('#Reserve').val());
        if((BuyItNowPrice <= StartingPrice) && (StartingPrice > 0)){
            alert("The 'Buy It Now' price must be higher...");
            return false;
        }
        if((Reserve <= StartingPrice) && (StartingPrice > 0)){
            alert("Your 'Reserve Price' must be higher...");
            return false;
        }
        return true;
    }
}

Question: How do I call it on blur? I tried this code below but it doesn't seem to work:

$('#StartingPrice').blur(function(){
    checkStartPrice();
});

I have this function, see below:

function checkStartPrice (){
    if ($('#StartingPrice')[0].value.length == 0){
        alert("The 'Starting Price' cannot be left empty!");
        return false;
     } else {
        var BuyItNowPrice = parseFloat($('#BuyItNowPrice').val());
        var StartingPrice = parseFloat($('#StartingPrice').val());
        var Reserve = parseFloat($('#Reserve').val());
        if((BuyItNowPrice <= StartingPrice) && (StartingPrice > 0)){
            alert("The 'Buy It Now' price must be higher...");
            return false;
        }
        if((Reserve <= StartingPrice) && (StartingPrice > 0)){
            alert("Your 'Reserve Price' must be higher...");
            return false;
        }
        return true;
    }
}

Question: How do I call it on blur? I tried this code below but it doesn't seem to work:

$('#StartingPrice').blur(function(){
    checkStartPrice();
});
Share Improve this question edited Feb 28, 2016 at 12:30 Rizier123 59.7k17 gold badges104 silver badges164 bronze badges asked Apr 26, 2011 at 12:19 NasirNasir 4,88511 gold badges36 silver badges39 bronze badges 3
  • 4 It will work, even $('#StartingPrice').blur(checkStartPrice) will do, if you correctly call it in the document.ready callback. The DOM has to be loaded for this to work. See api.jquery./ready – Felix Kling Commented Apr 26, 2011 at 12:21
  • What the type of the The #StartingPrice? It`s need to be of type that support blur event – liron Commented Apr 26, 2011 at 12:28
  • @Felix-Kling The error was somewhere else in my JS file...which cause the script not to work. Thanks – Nasir Commented Apr 26, 2011 at 12:54
Add a ment  | 

1 Answer 1

Reset to default 2

This is the correct way to call it.
The only reason it may fail is that the #StartingPrice element does not exist at the time of the .blur() call.

If it is present in the page, use this:

$(document).ready(function() {
    $('#StartingPrice').blur(checkStartPrice)
})

If it is dynamically added via AJAX, use this:

$(document).ready(function() {
    $('#StartingPrice').live('blur',checkStartPrice)
})

Note that the second solution requires at least jQuery 1.4.1

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论