引导帮助块仅在表单组有错误时显示

编程入门 行业动态 更新时间:2024-10-26 17:30:21
本文介绍了引导帮助块仅在表单组有错误时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个注释表单,该表单还将引导帮助块显示为:

I have a comment form that is showing also the bootstrap help-block as:

<p class="help-block">required</p>

我希望隐藏此p,除非提交表单并验证该字段为错误时.

I want this p to be hidden except when the form is submitted and the field is validated as an error.

提交表单时出错,父级<div class="form-group">...</div>将获得一个附加的has-error类.仅当表单提交中有错误时,我才能显示help-block吗?顺便说一句,我无权访问HTML ...

when the form is submitted with an error, the parent <div class="form-group">...</div> gets an additional has-error class. How could I show the help-block only if there is an error in the form submission? Btw I have no access to the HTML...

推荐答案

CSS方式

.help-block { display: none; } .form-group.has-error .help-block { display: block; }

jQuery方式

$('.help-block').hide(); $('#yourForm').submit(function(){ $.each($(this).find('.form-group'), function(){ // Long but clear version if ( $(this).hasClass('has-error') ) { $(this).find('.help-block').show(); } else { $(this).find('.help-block').hide(); } // Shortest version // $(this).find('.help-block')[ $(this).hasClass('has-error') ? 'show' : 'hide' ](); }); });

更多推荐

引导帮助块仅在表单组有错误时显示

本文发布于:2023-11-27 19:34:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   有错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!