[英]Jquery keyup on page load
I am using jquery keyup to calculate fees ,the keyup is working fine but the problem is on page load fees are not showing that means it is only works when a key is pressed but i want to calculate fees on page load.Here is my code 我正在使用jquery keyup来计算费用,该keyup正常工作,但问题是页面加载费用未显示,这意味着它仅在按下键时才有效,但我想计算页面加载费用。这是我的代码
HTML: HTML:
<tr>
<td><label for="amount">Amount</label></td>
<td><div class="input-container"><input name="amount" id="amount" value="100" type="text" /></td>
</tr>
<tr>
<td><label for="fee">Fee<font color="#CC0000">(5%)</font></label></td>
<td>$ <span id="fees"> </span>
</td>
</tr>
<tr>
<td><label for="net">Net amount</label></td>
<td>$<span id="net"></span>
</td>
</tr>
Jquery: jQuery的:
$(document).ready(function(){
$('#amount').on('keyup change', function(){
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
});
});
You can do something like this 你可以做这样的事情
$(document).ready(function(){
//calcualte fee on page load
calculateFee();
$('#amount').on('keyup change', function(){
calculateFee();
});
function calculateFee(){
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
}
});
Just trigger the "keyup" event explicitly. 只需显式触发“ keyup”事件。
$('#amount').trigger("keyup");
Thanks 谢谢
Something like this: 像这样:
$(document).ready(function(){
function calculate() {
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
}
$('#amount').on('keyup change', function(){
calculate();
});
calculate();
});
Move this code into its own function: 将此代码移到其自己的函数中:
function calculate()
{
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
}
Then do this: 然后执行以下操作:
$(document).ready(function()
{
calculate();
$('#amount').on('keyup change', function()
{
calculate();
});
});
So you call the calculate first on document ready then bind to the keyup event; 因此,您首先在文档准备就绪时调用计算,然后绑定到keyup事件;
Try to use this Javascript, you define function, which you will call on page load and then on each 'keyup change' event. 尝试使用此Javascript,然后定义函数,该函数将在页面加载时调用,然后在每次“键更改”事件时调用。
function calculateFees() {
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
}
$(document).ready(function(){
calculateFees();
$('#amount').on('keyup change', calculateFees);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.