必填属性HTML5 [英] Required attribute HTML5

查看:122
本文介绍了必填属性HTML5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的web应用程序中,我为表单域使用了一些自定义验证。在同一个表单中,我有两个按钮:一个实际提交表单,另一个取消/重置表单。

现在Safari 5已经出局,突然我的取消/重置按钮不再起作用了。每次我按下重置按钮时,表单中的第一个字段确实得到了焦点。但是,这与我的自定义表单验证行为相同。当用另一个浏览器尝试时,一切正常。我必须成为一个Safari 5的问题。



我在Javascript代码中改变了一下,我发现下面一行导致了这个问题:

  document.getElementById(somefield)。required = true; 

为了确保这个问题确实是我创建测试场景的问题:

 <!DOCTYPE html> 
< html>
< head>
< title>测试< /标题>
< / head>

< body>
< form id =someform>
< label>名称:< / label>& nbsp;< input type =textid =namerequired =true/>< br />
< label> Car:< / label>& nbsp;< input type =textid =carrequired =true/>< br />
< br />
< input type =submitid =btnsubmitvalue =提交! />
< / form>
< / body>
< / html>

我的预期会发生。



其他人偶然发现了这一点?

解决方案

我刚刚在Safari 5中遇到了这个问题,并且Opera10已经有一段时间了,但我从未花时间修复它。现在我需要修复它,看到你的帖子,但没有解决方案,但如何取消表单。经过多次搜索,我终于找到了一些东西:

http://www.w3.org/TR/html5/forms.html#attr-fs-formnovalidate

 < input type = submit formnovalidate name = cancel value =Cancel> 

适用于Safari 5和Opera 10。


Within my web application I am using some custom validation for my form fields. Within the same form I have two buttons: one to actually submit the form and the other to cancel/reset the form.

Mostly I use Safari as my default browser. Now Safari 5 is out and suddenly my cancel/reset button didn't work anymore. Every time I did hit the reset button the first field in my form did get the focus. However this is the same behavior as my custom form validation. When trying it with another browser everything just worked fine. I had to be a Safari 5 problem.

I changed a bit in my Javascript code and I found out that the following line was causing the problem:

document.getElementById("somefield").required = true;

To be sure that would be really the problem I created a test scenario:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>

<body>
    <form id="someform">
        <label>Name:</label>&nbsp;<input type="text" id="name" required="true" /><br/>
        <label>Car:</label>&nbsp;<input type="text" id="car" required="true" /><br/>
        <br/>
        <input type="submit" id="btnsubmit" value="Submit!" />
    </form>
</body>
</html>

What I expected would happen did happen. The first field "name" did get the focus automatically.

Anyone else stumbled into this?

解决方案

I just ran into this issue with Safari 5 and it has been an issue with Opera 10 for some time, but I never spent time to fix it. Now I need to fix it and saw your post but no solution yet on how to cancel the form. After much searching I finally found something:

http://www.w3.org/TR/html5/forms.html#attr-fs-formnovalidate

<input type=submit formnovalidate name=cancel value="Cancel">

Works on Safari 5 and Opera 10.

这篇关于必填属性HTML5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆