使用 jquery 的文本框 onchange 事件导致 JScript 运行时错误:'Deductions' 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9563204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:58:53  来源:igfitidea点击:

textbox onchange event using jquery causes JScript runtime error: 'Deductions' is undefined

jqueryonchange

提问by shanish

I have a query regarding calling jQuery for textbox onchange event.

我有一个关于为文本框 onchange 事件调用 jQuery 的查询。

in my coding am calling onchange event as

在我的编码中调用 onchange 事件作为

<asp:TextBox ID="txtTotalDeductions"  Text="0" runat="server" 
                                                        ClientIDMode="Static"     onChange="Deductions();" ></asp:TextBox>

and I have two div sections as

我有两个 div 部分

<div id="Total">1000</div>

and

<div id="NetTotal">0</div>

I need to calculate the "NetTotal" by subtracting the Total - txtTotalDeductions.

我需要通过减去总计 - txtTotalDeductions 来计算“NetTotal”。

and my jQuery for Deductions is

我的 jQuery for Deductions 是

//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 ""

当我运行应用程序时,错误显示为“Microsoft JScript 运行时错误:'Deductions' 未定义”,错误就在此处“”

can anyone help me out pls.....thanks in advance

任何人都可以帮助我..... 提前致谢

回答by John x

remove the OnChange handler

删除 OnChange 处理程序

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);

});

回答by Dhaval Shukla

Change your jScript to this, this will help :

将您的 jScript 更改为此,这将有所帮助:

$(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.

从文本框中删除您的 onchange 事件并删除函数推导。