jquery:试图获取文本字段的值.onfocus()

时间:2012-09-05 15:46:09

标签: jquery

我正在为购房者建造一个计算器。每次用户在字段中输入数字时,最终购买价格都会更新。有几个字段实际上占总购买价格的6倍变量。我希望6个月的计算仅在输入特定字段后显示。

目前有效,但无法计算正确的金额。但是,如果您回到文本字段并显示文本,则计算器可以正常工作。最终购买价格每次都会正确计算。

http://coyle.webedgemedia.com/Admin/BidSheet/47

http://jsfiddle.net/webedge/7fyV5/(CSS未正确通过,但您可以了解正在发生的事情)

我确定它与DOM有关,当我选择它时不知道值,但我认为.on()函数可以帮助解决这个问题。任何帮助表示赞赏。

$('.currency').on("blur", function () {

    var purchasePrice = 0;
    var futureSalesPrice = $('#txtFutureSales').length > 0 ? $('#txtFutureSales').val().replace("$", "").replace(",", "") : 0;
    var purchaseLoan = $('#txtPurchaseLoan').length > 0 ? $('#txtPurchaseLoan').val().replace("$", "").replace(",", "") : 0;
    var remodeling = $('#txtRemodeling').length > 0 ? $('#txtRemodeling').val().replace("$", "").replace(",", "") : 0;
    var utilities = $('#txtUtilities').length > 0 ? $('#txtUtilities').val().replace("$", "").replace(",", "") : 0;
    var HOADues = $('#txtHOADues').length > 0 ? $('#txtHOADues').val().replace("$", "").replace(",", "") : 0;
    var Insurance = $('#txtInsurance').length > 0 ? $('#txtInsurance').val().replace("$", "").replace(",", "") : 0;
    var Taxes = $('#txtTaxes').length > 0 ? $('#txtTaxes').val().replace("$", "").replace(",", "") : 0;
    var LoanInterestCarry = ('#txtLoanInterestCarry').length > 0 ? $('#txtLoanInterestCarry').val().replace("$", "").replace(",", "") : 0;
    var Inspection = $('#txtInspection').length > 0 ? $('#txtInspection').val().replace("$", "").replace(",", "") : 0;
    var SaleTitle = $('#txtSaleTitle').length > 0 ? $('#txtSaleTitle').val().replace("$", "").replace(",", "") : 0;
    var RealEstateSales = $('#txtRealEstateSales').length > 0 ? $('#txtRealEstateSales').val().replace("$", "").replace(",", "") : 0;
    var ProjectedProfit = $('#txtProjectedProfit').length > 0 ? $('#txtProjectedProfit').val().replace("$", "").replace(",", "") : 0;

    utilities = utilities * 6;
    HOADues = HOADues * 6;
    Insurance = Insurance * 6;
    Taxes = Taxes * 6;
    LoanInterestCarry = LoanInterestCarry * 6;

    $('.currency').formatCurrency();

    purchasePrice = futureSalesPrice - purchaseLoan - remodeling - Insurance - utilities - HOADues - Taxes - LoanInterestCarry - Inspection - SaleTitle - RealEstateSales - ProjectedProfit;

    $('#txtUtilities').on("blur", function () {
        $('.utilitiesMonthly').text(utilities).formatCurrency();
    });

    $('#txtHOADues').on("blur", function () {
        $('.hoaMonthly').text(HOADues).formatCurrency();
    });

    $('#txtInsurance').on("blur", function () {
        $('.insuranceMonthly').text(Insurance).formatCurrency();
    });

    $('#txtTaxes').on("blur", function () {
        $('.taxesMonthly').text(Taxes).formatCurrency();
    });

    $('#txtLoanInterestCarry').on("blur", function () {
        $('.loanInterestCarry').text(LoanInterestCarry).formatCurrency();
    });

    $('.price').text(purchasePrice).formatCurrency();

});

1 个答案:

答案 0 :(得分:0)

我从.on()函数中取出代码并决定在.keyup()上更新它。

$('#txtFutureSales').on("keyup", function () {
        $('#txtRealEstateSales').val(($(this).val().replace("$", "").replace(",", "")) * .06).formatCurrency();
    });

由于变量超出范围,因此使用$(this)来获取输入的值同样容易。

可能有更快的方法,但它的工作原理。我仍然会接受任何建议。