通过以编程方式更改文本框值来调用Javascript onchange事件

问题描述:

我面临的问题是:


  • 我在日历控件旁边有一个日期范围的文本框。

  • 当用户从日历中选择日期时,它会将该日期填入文本框中

  • 当发生这种情况时,我想触发一个javascript函数,然而,'onchange'事件似乎没有发生。

我希望能够将事件添加为文本框的属性,如:

I'd ideally like to be able to add the event as an attribute to the textbox, something like:

txtCreateDate.Attributes.Add("onchange", string.Format("JSfunction({0},'{1}');", arg1, arg2));

更多信息:
日期输入到c#的文本框中。日历控件在页面上有一个事件处理程序。选择日期后,事件处理程序会将日期放入文本框中。我已经尝试将焦点设置到文本框,然后在文本框上放置一个onBlur属性,但文本框似乎永远不会得到焦点。

more info: The date is entered into the textbox in c#. The calendar control has an eventhandler on the page. When a date is selected, the eventhandler puts the date into the textbox. I have tried setting the focus to the textbox at this point, then putting an onBlur attribute on the textbox, but the textbox never seems to get focus.

我想我可以此时直接调用javascript函数做ClientScript.RegisterClientScriptBlock或类似的东西,但这似乎很草率,从来没有像我想要的那样工作。

I suppose I could directly call the javascript function at this point doing ClientScript.RegisterClientScriptBlock or something like that, but that seems sloppy and never works like I want it to.

你错误解释了onchange事件在应用于textarea时的作用。直到它失去焦点或你进入后它才会开火。为什么不从填充文本区域的选择的onchange上激活函数?

You're misinterpreting what the onchange event does when applied to a textarea. It won't fire until it loses focus or you hit enter. Why not fire the function from an onchange on the select that fills in the text area?

在这里查看有关onchange事件的更多信息: w3schools

Check out here for more on the onchange event: w3schools