如何在 htmltextbox 中为 textchanged 事件调用 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11970116/
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
how to call javascript for textchanged event in htmltextbox?
提问by ksg
I've got two html textboxes (txtFrom
and txtTo
) which is read-only. These two textboxes are used to show dates from datepicker.
我有两个只读的html 文本框 (txtFrom
和txtTo
)。这两个文本框用于显示日期选择器中的日期。
My scenario is when txtTo
textbox is filled with date i want to call a javascript which subtracts the two dates (txtTo
- txtFrom
) and enter the result in another textbox named txtDays
.
我的场景是当txtTo
文本框充满日期时,我想调用一个 javascript 减去两个日期 ( txtTo
- txtFrom
) 并将结果输入另一个名为txtDays
.
How can i do this? I can't call onkeypress
or onkeyup
event since the textbox is read-only.
我怎样才能做到这一点?由于文本框是只读的,我无法调用onkeypress
或onkeyup
事件。
采纳答案by derki
you should have callback function on that datepicker, when user selects a date it could call this function
你应该在那个日期选择器上有回调函数,当用户选择一个日期时它可以调用这个函数
dont know if its yours or third party datepicker but he should have defined callback when user selects a date and than it writes it to the text input, you should be able to add your function at this place
不知道它是你的还是第三方的日期选择器,但他应该在用户选择日期时定义回调并将其写入文本输入,你应该能够在这个地方添加你的函数
回答by Adil
You can use onchange event of javascript.
您可以使用 javascript 的 onchange 事件。
In html Section
在 html 部分
<asp:TextBox ID="txtTo" runat="server" onchange="DateComparisionJavascriptFun();"></asp:TextBox>
In Javascript Block
在 Javascript 块中
<script type="text/javascript">
function DateComparisionJavascriptFun()
{
alert("Validate dates here.");
}
</script>
回答by Chris Jaynes
The onchange event only fires when the text field loses focus, so that probably won't work for you.
onchange 事件仅在文本字段失去焦点时触发,因此这可能对您不起作用。
If you're programatically changing the text of the date field, then you should probably just change the other fields at the same time.
如果您以编程方式更改日期字段的文本,那么您可能应该同时更改其他字段。
If you're going to be doing this sort of thing in more than one place, I'd highly recommend using a data binding framework like Knockout JS, and use a ViewModel with a computed property.
如果您要在多个地方做这种事情,我强烈建议您使用像Knockout JS这样的数据绑定框架,并使用具有计算属性的 ViewModel。