防止用户修改文本框中的日期
我有一个文本框,旁边有一个日历图标。单击该图标时,弹出窗体将显示日历控件。我将其设置为只能选择一周结束日期(星期六),并且该日期显示在文本框中。
我想阻止用户编辑文本框。我尝试过使用只读和启用属性,但这不起作用。
如何防止用户修改文本框中的日期?
(我正在使用该网站的 .Net 时间跟踪器入门工具包模板)
I have a textbox with a calendar icon next to it. When the icon is clicked a popup form displays the calendar control. I have it set up where they can only select the week ending date(saturday) and that date is displayed in the textbox.
I want to prevent users from editing the textbox. I've tried using the readonly and enabled properties but that doesn't work.
How can I keep users from modifying the date in the textbox?
(I'm using the .Net time tracker starter kit template for the site)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通过将文本框设置为服务器端的只读属性来实现此目的。
它可以工作,并且不依赖于 JavaScript。
I do this by setting the textbox as read-only property from server side.
It works, and it's non-JavaScript dependant.
尝试使用标签,其样式看起来像文本框。您还可以将 TextBox 的 Enabled 属性设置为
false
,但这可能对您不起作用,具体取决于您正在执行的操作。另一种方法可能是使用一些 JS 来防止值发生更改,但这可能会变得非常复杂。Try using a Label instead, styled to look like a TextBox. You can also set the TextBox's Enabled property to
false
, but that might not work for you depending on what you're doing. The alternative is probably using some JS to prevent changes to the value, but that could get pretty complicated.您可以在
onfocus
事件中调用 javascriptblur()
:You can call javascript
blur()
in theonfocus
event:我知道这是一个黑客行为,但您始终可以将选定的日期保存为变量。然后使用值更改时调用的动作侦听器。如果日期无效(即不是一周结束日期),则用保存的变量替换文本框。否则,如果日期值是用户更改的变量,则替换该变量。
I know it is a hack but you could always save the selected date as a variable. Then use an actionlistener that is called when the value is changed. If the date is invalid (ie. Not a week ending date) then replace the textbox with the saved variable. Otherwise if the date is value the replace the variable for if the user changes it.
如果用户不应该编辑文本框中的文本,则不应使用文本框。
使用标签。
If the user isn't supposed to edit the text in the textbox you shouldn't be using a textbox.
Use a label.