admin管理员组

文章数量:1391969

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

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

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

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...

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

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

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

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...

Share Improve this question edited Oct 11, 2016 at 12:11 Ali Hesari 1,9495 gold badges27 silver badges54 bronze badges asked Oct 11, 2016 at 11:22 TorostarTorostar 4972 gold badges8 silver badges20 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

CSS way

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

jQuery way

$('.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' ]();
  });
});

本文标签: javascriptbootstrap helpblock to show only if formgroup haserrorStack Overflow