1

I have a problem with bootstrap, validate and data-loading-text. When you click on save and not all fields are correct the button must be reset "$('#save').button('reset');". Also "This field is required." is put on the field. I do this by "error.insertAfter(element);" See the code below on validate.

Everything is working one time. When I click the second time on the save button (still the field not filled in) the button is not reset anymore.

How can I solve this?

<button type="submit" id="save" name="save" value="ok" class="btn btn-success btn-fill btn-wd" data-loading-text="<i class='fa fa-spinner fa-spin'></i> SAVING"> SAVE</button>

<script>
  $('#save').on('click', function() {
      var $this = $(this);
      $this.button('loading');
  });
</script>

...

<script>
$(document).ready(function () {

var $validator = $("#new").validate({
  rules: {
    test: {
      required: true,
    }
  },
  errorPlacement: function(error, element) {
    $('#save').button('reset');
    error.insertAfter(element);
 }, 
});
}
</script>
Prince Sodhi
  • 2,845
  • 2
  • 21
  • 35
Lewis
  • 145
  • 2
  • 2
  • 8
  • You're still not clear. Don't you think your `` tag should be covering all the functions and variable? – StackUseR Aug 28 '17 at 10:53
  • Of course are there – Lewis Aug 28 '17 at 12:52

2 Answers2

1

I have solved it by change errorPlacement for submitHandler.

<script>
$(document).ready(function () {

  var $validator = $("#new").validate({
    rules: {
      test: {
        required: true,
      }
    },
    submitHandler: function(validator, form, submitButton) {
      $('#save').button('loading');
      validator.defaultSubmit();
    },
  });
}
</script>
Lewis
  • 145
  • 2
  • 2
  • 8
-1

First, I don't know any function called 'button' in Javascript that you are using.

Second, all javascript code should be within tag.

Third, import jquery before you write onclick function.

It has a answer here, Check this

Kirsten Phukon
  • 314
  • 3
  • 17
  • Same problem when I use import jquery before the write onclick function. The fist click it works but the second the button isn’t reset. – Lewis Aug 28 '17 at 13:50
  • Then it must be a different problem that does not reflect in code u have given :) – Kirsten Phukon Aug 30 '17 at 06:50