onkey 事件 - 禁用按钮的问题
说明: 开始时该字段的值为 YYYY-MM-DD。如果用户删除该值并且不输入任何内容,则应禁用“确定”按钮。如果用户删除该值并输入新值,则应启用“确定”按钮。该代码仅适用于第二种情况。
function ChangeOkButton()
{
if(document.getElementById('fromDate').value == null)
{ document.getElementById('save').disabled = true; }
else {document.getElementById('save').disabled = false; }
}
<input type="text" name="fromDate" id="fromDate" value="YYYY-MM-DD" onkeypress="ChangeOkButton();"/>
这可能吗?
谢谢你!
Explanation: At the beginning the value of the field is YYYY-MM-DD. if the user delete the value and doesn't type anything, the button "ok" should be disabled. if the user delete the value and type new value, the button "ok" should be enable. The code is working only for the second case.
function ChangeOkButton()
{
if(document.getElementById('fromDate').value == null)
{ document.getElementById('save').disabled = true; }
else {document.getElementById('save').disabled = false; }
}
<input type="text" name="fromDate" id="fromDate" value="YYYY-MM-DD" onkeypress="ChangeOkButton();"/>
Is this possible?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该函数对于此类控制不是很有用,因为您可以用“12345”、“foobar”或与实际值不同的其他内容覆盖该值。我想您想要从 2000-01-01 开始的日期
,并且您的输入是
请注意,我没有考虑闰年或每月的天数,这只是对用户输入的数据类型的更可靠的控制。根据需要更改正则表达式
注意:出于性能考虑,考虑将 'okbtt' 变量放在函数外部,否则每次调用此函数时都需要获取引用。可怕。
That function is not very useful for that kind of control, since you could overwrite the value with '12345', 'foobar' or something else different than a realistic value. I suppose you want date starting from 2000-01-01
and your input is
Please note I've not considered leap years or days per month, this is only a more reliable control on data type entered by the user. Change the regexp as you like
Note: consider to put the 'okbtt' variable outside to the function for a matter of performance, otherwise you need to obtain a reference each time you call this function. awful.