Html HTML5 必需属性似乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17966390/
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
HTML5 required attribute seems not working
提问by HTTP
I can't figure out why the new requiredattribute of HTML5 seems to not be working, and I know my simple code seems to be okay. What should I do to make this work?
我不明白为什么HTML5的新required属性似乎不起作用,而且我知道我的简单代码似乎没问题。我应该怎么做才能使这项工作?
Here is my code in HTML:
这是我的 HTML 代码:
<input type = "text" class = "txtPost" placeholder = "Post a question?" required>
<button class = "btnPost btnBlue">Post</button>
Correct me if I'm wrong but, if ever I run the code in the browser and click the button without any value in the textbox it should have a tooltip showing that that field was required, shouldn't it? But nothing happens no matter how many times you click the button. Am I misunderstood on how to use the requiredattribute?
如果我错了,请纠正我,但是,如果我在浏览器中运行代码并单击文本框中没有任何值的按钮,它应该有一个工具提示,显示该字段是必需的,不是吗?但是无论您单击该按钮多少次都没有任何反应。我是否误解了如何使用required属性?
I am running my code in google chrome Version 28.0.1500.72
.
我在 google chrome 中运行我的代码Version 28.0.1500.72
。
回答by LDJ
Try putting it inside a form tag and closing the input tag:
尝试将其放入表单标签并关闭输入标签:
<form>
<input type = "text" class = "txtPost" placeholder = "Post a question?" required />
<button class = "btnPost btnBlue">Post</button>
</form>
回答by Jinu Joseph Daniel
Make sure that novalidate attribute is not set to your form tag
确保没有将 novalidate 属性设置为您的表单标记
回答by Ram Kowshik
Actually speaking, when I tried this, it worked only when I set the action and method value for the form. Funny how it works though!
实际上,当我尝试此操作时,它仅在我为表单设置操作和方法值时才起作用。有趣的是它的工作原理!
回答by Kamal AZIZ
My answer is too late, but it can help others.
我的回答为时已晚,但它可以帮助其他人。
I had the same problem, even when I used a form tag.
我遇到了同样的问题,即使我使用了表单标签。
I solved it by declaring Meta Charset in the header of the page:
我通过在页面标题中声明 Meta Charset 来解决它:
<meta charset = "UTF-8" />
回答by Ardian Zack
using form encapsulation and add your button type"submit"
使用表单封装并添加您的按钮类型“提交”
回答by VINOTH KUMAR
Absence of Submit field element in the form also causes this error. In the case of "button" field handled by JS to submit form lacks the necessity of Submit button hence Required doesn't Work
表单中缺少 Submit 字段元素也会导致此错误。对于由 JS 处理提交表单的“按钮”字段缺乏提交按钮的必要性,因此必需不起作用
回答by speti43
回答by Diwakar Sharma
Post
邮政
Put this in form tag and method is POST
把它放在表单标签中,方法是 POST
If using MVC then check from your jquery if text is not fill then return it automatically show the message
如果使用 MVC,则从您的 jquery 检查文本是否未填充然后返回它自动显示消息