我有一个表单(jconfirm内容的一部分)有一个输入字段。用户可以更改输入值,并且根据余额更改后的值,需要根据updateOfflineAmount()中的计算来更新脱机量。现在,在更改了余额字段的输入字段之后,不会更新离线金额。
$.confirm({
title:'',
content:'<form action="" class="formName">' +
'<div class="form-group">' +
'<div id="account_balance">account balance: $'+account_balance+'</div>' +
'<div id="total_amount">total amount: $'+total_amount+'</div>' +
'<div id="balance_amount">balance amount: <input type="number" id="balance_amount" name="balance_amount" value='+balance_amount+'></div>'+
'<div id="offline_amount">offline amount: $'+offline_amount+'</div>' +
'</div>' +
'</form>'
});
function updateOfflineAmount(){
var balance_amount_tmp=$('#balance_amount').val();
offline_amount=total_amount-balance_amount_tmp;
$("#offline_amount").val(offline_amount);
}
$('body').on('change','#balance_amount',function() {
updateOfflineAmount();
});
答案 0 :(得分:0)
在这种情况下,您必须使用on()
方法。您可以使用
$('body').on('change','#balance_amount', function() {
updateOfflineAmount();
});
我希望这会对您有所帮助。
答案 1 :(得分:0)
在添加的脚本标记中
$(document).ready(function(){
// jQuery methods go here... or all js and jquery code here
});
jquery起始语法
答案 2 :(得分:0)
您将ID
加倍,并在html()
和val()
之间混合,使用parseInt()
从html中获取整数,最好使用input
事件
var account_balance = 999,
total_amount = 666,
balance_amount = 0,
offline_amount = 0,
myForm = '<form action="" class="formName">' +
'<div class="form-group">' +
'<div id="account_balance">account balance: $<span id="total_amount">' + account_balance + '</span></div>' +
'<div>total amount: $' + total_amount + '</div>' +
'<div>balance amount: <input type="number" id="balance_amount" name="balance_amount" value=' + balance_amount + '></div>' +
'<div>offline amount: $<span id="offline_amount">' + offline_amount + '</span></div>' +
'</div>' +
'</form>'
$('body').html(myForm);
function updateOfflineAmount() {
var balance_amount_tmp = $('#balance_amount').val(),
total_amount = parseInt($('#total_amount').html());
$('#balance_amount').val(balance_amount_tmp);
offline_amount = total_amount - balance_amount_tmp;
$("#offline_amount").html(offline_amount);
}
$('#balance_amount').on('input', function() {
updateOfflineAmount();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>