网络营销师马栓网络营销师马栓网络营销师马栓

input如何设置文本框必填项以及修改提示文字的方法

<p style="font-family: "microsoft yahei"; margin-top: 0px; margin-bottom: 0px; padding: 0px 0px 0.5em; list-style-type: none; clear: both; text-indent: 2em; line-height: 1.8em; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><strong style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;">如何在html网页中设置文本框为必填?</strong></span></p><p style="font-family: "microsoft yahei"; margin-top: 0px; margin-bottom: 0px; padding: 0px 0px 0.5em; list-style-type: none; clear: both; text-indent: 2em; line-height: 1.8em; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;">html5新增了一个required属性,可以使用这个属性对文本框设置必填项,直接在input文本框上添加required即可 。<br style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></p><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;">语法:<input type="text"&nbsp; required/></span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><br type="_moz" style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;">以韩国中央大学中韩国际班网站:https://www.xxxxx.com/hdbm/ 表单为例设置必填项,在input文本框中添加required属性。</span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-size: 16px;">&nbsp;</span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><img src="http://www.mashuan.com/uploads/allimg/190525/1_190525005521_1.jpg" width="863" border="0" height="461" alt="" data-bd-imgshare-binded="1" style="font-size: 19.2px; margin: 0px auto; padding: 0px; list-style-type: none; border: 0px; width: 738px; cursor: pointer; display: block !important;"/><br style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><br type="_moz" style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><br type="_moz" style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></div><div style="font-family: "microsoft yahei"; margin: 0px; padding: 0px; list-style-type: none; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><img src="http://www.mashuan.com/uploads/allimg/190525/1_190525005542_1.jpg" width="898" border="0" height="562" alt="" data-bd-imgshare-binded="1" style="font-size: 19.2px; margin: 0px auto; padding: 0px; list-style-type: none; border: 0px; width: 738px; cursor: pointer; display: block !important;"/><br style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></div><p style="font-family: "microsoft yahei"; margin-top: 0px; margin-bottom: 0px; padding: 0px 0px 0.5em; list-style-type: none; clear: both; text-indent: 2em; line-height: 1.8em; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><br type="_moz" style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;"/></span></p><p style="font-family: "microsoft yahei"; margin-top: 0px; margin-bottom: 0px; padding: 0px 0px 0.5em; list-style-type: none; clear: both; text-indent: 2em; line-height: 1.8em; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><strong style="font-size: 19.2px; margin: 0px; padding: 0px; list-style-type: none;">自定义required的提示文字的方法</strong></span></p><p style="font-family: "microsoft yahei"; margin-top: 0px; margin-bottom: 0px; padding: 0px 0px 0.5em; list-style-type: none; clear: both; text-indent: 2em; line-height: 1.8em; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;"><input type="text" required oninvalid="setCustomValidity(&#39;不能为空aaa&#39;)" oninput="setCustomValidity(&#39;&#39;)"></span></p><p style="font-family: "microsoft yahei"; margin-top: 0px; margin-bottom: 0px; padding: 0px 0px 0.5em; list-style-type: none; clear: both; text-indent: 2em; line-height: 1.8em; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; list-style-type: none; font-size: 16px;">HTML5表单验证给前端开发带来许多便利,但是默认的提示不友好。我们可以通过setCustomValidity方法来自定义提示信息,更准确的提示给用户。 在用户输入的时候使用setCustomValidity(&#39;&#39;)将错误提示设置为空字符串,在输入不合法时用setCustomValidity(&#39;自定义提示信息&#39;)来替换提示信息,这样写会有些问题就是如果用户没有输入那么还是默认的提示信息,所以可以用任何方式获取到这个元素,然后调用它的setCustomValidity()方法先将提示信息设置为空字符串</span></p><p><br/></p>

网络营销师马栓 » input如何设置文本框必填项以及修改提示文字的方法

部分文章来源互联网,出于非商业性学习目的,文章版权归原作者所有。