admin 管理员组

文章数量: 1086019

I have an asp:TextBox with asp:RegularExpressionValidator to validate if it's a number. Obviously an onchange event will be attached to this textbox while rendering. Also I add a change event at $(document).ready to make some calculation when the value is changed.

<asp:TextBox id="myText" runat="server" />
<asp:regularexpressionvalidator id="myRev" ControlToValidate="myText" runat="server">*</asp:regularexpressionvalidator>

$(document).ready(function(){
    $('[id$=myText]').bind('change',function(){
           //do something
    }).change();      //force the change event at the very beginning
});

My function will be executed later than the generated js because of the register time. But the asp js throws an error. I traced in the js:

   function ValidatorOnChange(event) {
       ...
   }

and found that all of event.fromElement,event.toElement,event.srcElement are null which causes the exception. Did I do something wrong? Any solutions? Thanks.

EDIT

It's proved to be a MS bug, working fine in ASP.NET 4 vs2010.

I have an asp:TextBox with asp:RegularExpressionValidator to validate if it's a number. Obviously an onchange event will be attached to this textbox while rendering. Also I add a change event at $(document).ready to make some calculation when the value is changed.

<asp:TextBox id="myText" runat="server" />
<asp:regularexpressionvalidator id="myRev" ControlToValidate="myText" runat="server">*</asp:regularexpressionvalidator>

$(document).ready(function(){
    $('[id$=myText]').bind('change',function(){
           //do something
    }).change();      //force the change event at the very beginning
});

My function will be executed later than the generated js because of the register time. But the asp js throws an error. I traced in the js:

   function ValidatorOnChange(event) {
       ...
   }

and found that all of event.fromElement,event.toElement,event.srcElement are null which causes the exception. Did I do something wrong? Any solutions? Thanks.

EDIT

It's proved to be a MS bug, working fine in ASP.NET 4 vs2010.

Share Improve this question edited Sep 10, 2012 at 15:02 Cheng Chen asked May 17, 2010 at 10:19 Cheng ChenCheng Chen 43.5k17 gold badges117 silver badges183 bronze badges 8
  • Do you need the handler to run, or just the jQuery one? – Nick Craver Commented May 17, 2010 at 10:20
  • @Nick Craver:$(XXX).change() will trigger all its change events. But why the generated js gives an error? I think it should be fired correctly. – Cheng Chen Commented May 17, 2010 at 10:24
  • Instead of $('[id$=myText]') use $('#myText') – Pointy Commented May 17, 2010 at 10:25
  • The errors are probably happening because jQuery doesn't set up the global event object when you simulate an event. (Guessing.) – Pointy Commented May 17, 2010 at 10:26
  • @Pointy - It's asp, *#$%ed up IDs when generated, at least until 4.0. @Danny - I know how .change() works, I was asking if you needed/wanted the .Net event handler to run :) – Nick Craver Commented May 17, 2010 at 10:27
 |  Show 3 more ments

2 Answers 2

Reset to default 5

Including Pointy's point (I crack myself up) about ID, you can re-write it like this:

$(function(){
  $('#<%=myText.ClientID%>').change(function() {
    //stuff
  }).triggerHandler('change');
});

Without seeing exactly how your other event is attached, .triggerHandler() would be my best suggestion, as the event doesn't bubble up for capture by the .Net handler.

Pack your calculations in a function and call it on ready event instead of triggering a change:

$(document).ready(function(){
    $('[id$=myText]').bind('change',function(){
           doCalc(); // or doCalc(this) or whatever you need
    });
    doCalc();
});

本文标签: cTrigger the change event of a textbox in jQueryStack Overflow