使用 JQuery 简单计算输入字段的百分比
我正在构建一个 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="[email protected]"/>
<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 →"/>
<input type="hidden" name="item_number" value="Item ID"/>
<input type="submit" name="submit" value="Confirm & Pay →" 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"/> 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>
I'm building a PayPal form that the user enters the amount and after that transfered to paypal,
everything is working fine but i need to add a calculation plugin to do the following,
If a user enters in the field 10$ (Calculate 10$-2%=8$) and return the result as text inside a span 8$
At the end of the form i have a "(p) paragraph" with a span id "site_commission_box" inside.
I need to show the calculated number inside this span.
Which JQuery plugin is good for use on this and how i can use it?
Any good example or tutorial to find out how i can do it?
Thanks a lot,
Philip
<form id="form_paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="business" value="[email protected]"/>
<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 →"/>
<input type="hidden" name="item_number" value="Item ID"/>
<input type="submit" name="submit" value="Confirm & Pay →" 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"/> Transferring to PayPal, please wait...</span>
<p>-2% of the price it will be <b>lt;span class="text_decoration" id="site_commission_box"></span> USD</b>.</p>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用
jQuery()
而不是$()
是否有原因?另外,您确实应该缓存所选元素,这样您就不必多次查询 DOM 来获取同一元素。
我将这样做:
您可以在此处看到一个工作示例,在那里您可以看到我也在 HTML 中做了更改。
Is there a reason you use
jQuery()
instead of$()
?Also, you should really cache your selected elements so you don't have to query the DOM multiple times for the same element.
Here's how I would do it:
You can see a working example here, there you can see the changes I did in the HTML as well.
您必须为更改事件添加事件处理程序。每次如果输入值更改,折扣都会重新计算。请参阅 http://jsfiddle.net/3sXZw/ 上的示例
You have to add a event-handler for a change event. Everytime if the input value is changed the discount is recalculated. See an example on http://jsfiddle.net/3sXZw/