Flask: update form value at JavaScript code

2021. 8. 14. 23:35Frontend/Web programming

    목차
반응형

A form can be updated wih the new value at JavaScript code by accessing the form's id through DOM tree element.

<form method="post" action="">
    {{ form.hidden_tag() }}
    <fieldset class="form-group">
    <div class="form-group row">
        <div id="id_form_name" class="col-xs-2">
            {{ form.name.label(class="form-control-label") }}
            ...

in the HTML code, name form has its DOM element id as 'name'.
Thus, in the JavaScript code, 'name' is used as DOM element id.

function update_info(data) {  
    document.getElementById('name').value = data.name  
    ...  
}

$.ajax({  
  type : "POST",  
  url : ...  
  dataType: "json",  
  data: JSON.stringify(selected_row_data),  
  contentType: 'application/json',  
  success: function(data) {  
      update_info(data["_info"])  
  },  
  error: function(xtr, status, error) {  
      console.log("error @ ajax call")  
  }  
});
반응형