最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - how to call javascript for textchanged event in htmltextbox? - Stack Overflow

programmeradmin3浏览0评论

I've got two html textboxes (txtFrom and txtTo) which is read-only. These two textboxes are used to show dates from datepicker.

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.

How can i do this? I can't call onkeypress or onkeyup event since the textbox is read-only.

I've got two html textboxes (txtFrom and txtTo) which is read-only. These two textboxes are used to show dates from datepicker.

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.

How can i do this? I can't call onkeypress or onkeyup event since the textbox is read-only.

Share Improve this question edited Aug 15, 2012 at 13:30 Magnus 47k8 gold badges85 silver badges124 bronze badges asked Aug 15, 2012 at 13:26 ksgksg 4,0678 gold badges53 silver badges98 bronze badges 1
  • Why are you using text boxes for a read only field? – Des Horsley Commented Apr 28, 2013 at 22:27
Add a ment  | 

3 Answers 3

Reset to default 4

You can use onchange event of javascript.

In html Section

<asp:TextBox ID="txtTo" runat="server" onchange="DateComparisionJavascriptFun();"></asp:TextBox>

In Javascript Block

<script type="text/javascript">
function DateComparisionJavascriptFun()
{
   alert("Validate dates here.");
}    
</script>

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

The onchange event only fires when the text field loses focus, so that probably won't work for you.

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 remend using a data binding framework like Knockout JS, and use a ViewModel with a puted property.

发布评论

评论列表(0)

  1. 暂无评论