问题
I have a query regarding calling jQuery for textbox onchange event.
in my coding am calling onchange event as
<asp:TextBox ID="txtTotalDeductions" Text="0" runat="server"
ClientIDMode="Static" onChange="Deductions();" ></asp:TextBox>
and I have two div sections as
<div id="Total">1000</div>
and
<div id="NetTotal">0</div>
I need to calculate the "NetTotal" by subtracting the Total - txtTotalDeductions.
and my jQuery for Deductions is
//Calculate deductions.
function Deductions() {
var result = new Object();
result.total = $("#Total").html();
result.totalDeductions = $("#txtTotalDeductions").val();
result.netTotal = result.total - result.totalDeductions;
$('#NetTotal').html(result.netTotal);
}
and when I run the application the error shows like "Microsoft JScript runtime error: 'Deductions' is undefined", and the error lies here ""
can anyone help me out pls.....thanks in advance
回答1:
remove the OnChange handler
function Deductions() {
var result = new Object();
result.total = $("#Total").html();
result.totalDeductions = $("#txtTotalDeductions").val();
result.netTotal = result.total - result.totalDeductions;
$('#NetTotal').html(result.netTotal);
}
also wrap the code inside the ready handler and attach a change event handler
$(document).ready(function(){
//attach with the id od deductions
$("#txtTotalDeductions").bind("change",Deductions);
});
回答2:
Change your jScript to this, this will help :
$(function (){
$('#txtTotalDeductions').change(function (){
var total = $("#Total").html();
var totalDeductions = $("#txtTotalDeductions").val();
var netTotal = total - totalDeductions;
$('#NetTotal').html(netTotal);
});
});
you can also use object. i have removed it for my convinience.
edit:
Remove your onchange event from textbox and also remove function deduction.
来源:https://stackoverflow.com/questions/9563204/textbox-onchange-event-using-jquery-causes-jscript-runtime-error-deductions-i