admin管理员组文章数量:1405636
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
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:
$(document).ready(function(){
$('#amount').on('keyup change', function(){
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
});
});
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
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:
$(document).ready(function(){
$('#amount').on('keyup change', function(){
$('#fees').text($('#amount').val()*(5/100));
$('#net').text($('#amount').val() - ($('#amount').val()*5/100));
});
});
Share
Improve this question
asked Dec 6, 2014 at 21:04
user2252617user2252617
991 gold badge4 silver badges14 bronze badges
5 Answers
Reset to default 4You 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.
$('#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;
Try to use this Javascript, you define function, which you will call on page load and then on each 'keyup change' event.
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);
});
本文标签: javascriptJquery keyup on page loadStack Overflow
版权声明:本文标题:javascript - Jquery keyup on page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744335781a2601182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论