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

Call Javascript onchange event by programmatically changing textbox value(通过以编程方式更改文本框值来调用 Javascript onchange 事件)
本文介绍了通过以编程方式更改文本框值来调用 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

这篇关于通过以编程方式更改文本框值来调用 Javascript onchange 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)