sunmingtao / sample-code

3 stars 4 forks source link

jquery $form.submit() doesn't work. It always navigats to /undefined #298

Closed sunmingtao closed 2 years ago

sunmingtao commented 2 years ago

html:

<a id="check-item-button" class="btn btn-success check-btn navbar-btn pull-right" href="#">Check Items <i class="fas fa-arrow-right"></i></a>

<form id="gen-single-upload-file-option-form" action="jobs/singleupload/${job.id!}/saveCheck" method="POST">
  <input type="radio" name="fileOption" value="attachToThisWork"> Attach file to this item
  <input type="radio" name="fileOption" value="replaceOrAddNewChildren"> Replace or add new children
</form>

jQuery:

$('#check-item-button').click(function(e) {
  e.preventDefault();
  $('#gen-single-upload-file-option-form').submit();
});

image

sunmingtao commented 2 years ago

Turns out there is a click event on class check-btn, which redirects the page to attribute target, which happens to be undefined for this link.

The solution is remove the class and add $(this).spinAndDisable(); to the click event on $('#check-item-button')

$('.check-btn').one('click', function(){
  $(this).spinAndDisable();
  window.location.href = $(this).attr('target');
});