javascript - 使用 JQuery 计算输入字段的简单百分比

标签 javascript jquery forms paypal jquery-calculation

我正在构建一个 PayPal 表单,用户输入金额,然后转账到 paypal,
一切正常,但我需要添加一个计算插件来执行以下操作,

如果用户在字段 10$ 中输入(计算 10$-2%=8$)并将结果作为跨度 8$ 内的文本返回

在表格的末尾,我有一个“(p) 段落”,里面有一个跨度 ID“site_commission_box”。
我需要在此跨度内显示计算出的数字。

哪个 JQuery 插件适用于此,我该如何使用它?
任何好的例子或教程来找出我该怎么做?

非常感谢,
菲利普

<form id="form_paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="business" value="mail@url.url"/>
    <input type="hidden" name="cmd" value="_xclick"/>
    <input type="hidden" name="return" value="http://returnpage.url"/>
    <input type="hidden" name="cancel_return" value="http://cancelreturnpage.url"/>
    <input type="hidden" name="rm" value="2"/>
    <input type="hidden" name="currency_code" value="USD"/>
    <input type="hidden" name="quantity" value="1"/>
    <input type="hidden" name="item_name" value="Item Name"/>
    <input type="hidden" name="item_number" value="Item ID"/>

    <label>Amount (US$) : </label>
    <input type="text" name="amount" id="input_amount" class="text" />

    <input type="hidden" name="invoice" value="Post ID"/>
    <input type="hidden" name="cbt" value="Make Payment &rarr;"/>
    <input type="hidden" name="item_number" value="Item ID"/>
    <input type="submit" name="submit" value="Confirm & Pay &rarr;" class="submit" />
    <br/>
    <span id="msg_moreamount" class="icon_warning red" style="display:none;">PayPal takes $0.35 commission for a $1 donation.</span>
    <span id="msg_noamount" class="icon_warning red" style="display:none;">Please enter an amount and try again.</span>
    <span id="msg_activity" style="display:none;"> <img src="loader.gif" align="middle" alt="load"/>&nbsp;Transferring to PayPal, please wait...</span>

    <p>-2% of the price it will be <b>$<span class="text_decoration" id="site_commission_box"></span> USD</b>.</p>
</form>

最佳答案

您使用 jQuery() 而不是 $() 有什么原因吗?

另外,你真的应该缓存你选择的元素,这样你就不必为同一个元素多次查询 DOM。

这是我的做法:

$(function() {
    // the minimum required value to be entered.
    // in this case PayPal takes $0.35 from a $1
    // donation, hence we ask for at least $1.35
    var minimum_value = 1.35;

    // cache elements that are used at least twice
    var $amount = $("#input_amount"),
        $msg = $("#msg"),
        $commission = $("#site_commission_box");

    // attach handler to input keydown event
    $amount.keyup(function(e){
        if (e.which == 13) {
            return;
        }
        var amount = parseFloat($amount.val()),
            commission = amount*0.02;

        if (isNaN(commission) || isNaN(amount)) {
            $msg.hide();
            $commission.hide();
            return;
        }

        if (amount <= minimum_value) {
            $commission.hide();
            $msg
                .text("PayPal takes $0.35 commission for a $"+amount+" donation.")
                .fadeIn();
        } else {
            $msg.hide();
            $commission
                .fadeIn()
                .find("span")
                    .text((amount-commission).toFixed(2));
        }
    });

    // attach handler to the form submit event
    $('#form_paypal').submit(function() {
        // check if there is an amount entered
        if ($amount.val() > null) {
            // is the amount equal to or higher than the minimum_value?
            if ($amount.val() < minimum_value) {
                // need more amount
                // show more amount error
                $msg
                    .addClass("icon_warning_red")
                    .text("Please enter an amount and try again.")
                    .fadeIn();
                return false; // prevent the form from submitting
            }
            else {
                // amount is more than minimum_value
                // show activity
                $msg
                    .removeClasss("icon_warning_red")
                    .html('<img src="loader.gif" align="middle" alt="load"/>&nbsp;Transferring to PayPal, please wait...')
                    .fadeIn();
                return true; // submit the form
            }
        }
        else {
            // no amount entered at all
            // show no amount error;
            $msg.addClass("icon_warning_red").fadeIn();
            return false; // prevent the form from submitting
        }
    });
});

您可以看到一个工作示例 here ,您还可以看到我在 HTML 中所做的更改。

关于javascript - 使用 JQuery 计算输入字段的简单百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4638847/

相关文章:

javascript - Angular 5 ngModel 不更新值

javascript - 动态更改文本,如何不删除 <i> 元素?

javascript - HTML5 多个文件上传 : Set Filenames with JavaScript?

javascript - Rails 3 远程表单不传递参数

c# - RichTextBox 和 Tab 键

javascript - 设置只读Ckeditor

javascript - Flow 中的可迭代类

javascript - 在 Vue 项目中包含或初始化 jQuery 库的最佳方法是什么?

javascript - 为什么我无法使用 jQuery 访问元素的数据属性。

jquery - AJAX ASYNC False 与 True