jQuery:val()返回输入字段初始值,而不是实际输入

我在弹出窗口中有一个输入表单,当我尝试检索输入值时,我会收到该值,该输入预先填充了(当前{{cleaning.frequency}})。弹出窗口是否可能是原因?

HTML:

    <td class="text-md-center with-popover">
      {{ cleaning.frequency }}
      <div id="frequency{{cleaning.id}}" style="display: none">
        <form method="POST">
         {% csrf_token %}
         <label for="cleaning_frequency">Frequency: </label>
         <input id="cleaning_frequency{{cleaning.id}}" type="number" name="frequency" value="{{cleaning.frequency}}"> 
         <button type="submit" class="btn btn-success btn-sm update-cleaning" name="cleaning_frequency" data-model="Cleaning" data-field="frequency" value="{{cleaning.id}}"> Change </button>
        </form>
      </div>
    </td>

JS:

$(document).on("click", ".update-cleaning", function(event){
  event.preventDefault();
  var obj_id = $(this).attr('value')
  var input_field_name = $(this).attr('name')
  var model_name = $(this).attr('data-model')
  var field_name = $(this).attr('data-field')
  var value = $("#" + input_field_name + obj_id).val()
  if (!value) {
      alert('Fill the field!')
    }
  else {
  $.ajax({
            url: "{% url 'ajax_update_cleaning' %}",
            type: "POST",
            data: {'object_id': obj_id, 'model_name': model_name, 'field_name': field_name, 'value': value, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
            dataType: 'json',
        })
        .done(function(response){
                    console.log(response);
                })
        }
});

任何帮助表示赞赏。谢谢。

评论