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

2022-01-13 00:00:00 textbox 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

相关文章