can not submit value of input hidden

2019-08-09 23:34发布

问题:

I have a form like this:

$(document).ready(function() {
  $('#create_lop_monhoc_modal').on('show.bs.modal', function(event) {
    var button = $(event.relatedTarget)
    var tenmh = button.data('tenmh')
    var mamh = button.data('mamh')
    var modal = $(this)
    modal.find('#input_tenmh').val(tenmh).trigger("change")
    modal.find('#tenmh').text(tenmh).trigger("change")
    modal.find('#input_mamh').val(mamh).trigger("change")
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="modal fade " id="create_lop_monhoc_modal">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <form action="/monhoc" method="POST">
        <input type="hidden" value="" id="input_mamh" name="mamh" />
        <input type="hidden" value="" id="input_tenmh" name="tenmh" />
        <div class="modal-body">
          <h4 id="mamh"></h4>
          <div>
            <div class="modal-footer">
              <button type="submit" class="btn btn-success">Crate</button>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

<button type="button" data-toggle="modal" data-target="#create_lop_monhoc_modal" data-tenmh="tenmh" data-mamh="mamh">                   
  Open Modal
</button>

When i click 'Open Modal', the value of input hidden was put, but when i submit that form, the value of it is not submit. when i click back button of browser and submit again, this submit success!.

I dont know why, please help!!

回答1:

I think my problem is caused by another javascript library, namely due to bootstrap-datepicker.js. Because when i click datepicker input, value of it is reset.

I solved this problem by remove datepicker and try submit again, and it is working. In my question, i think I think that .trigger("change") is no longer needed:

modal.find('#input_tenmh').val('tenmh').trigger("change")
modal.find('#tenmh').text('tenmh').trigger("change")
modal.find('#input_mamh').val('mamh').trigger("change")

after that:

modal.find('#input_tenmh').val('tenmh')
modal.find('#tenmh').text('tenmh')
modal.find('#input_mamh').val('mamh')

Thank for all!