JQuery how to submit only changed fields when the form has some data array

后端 未结 3 1559

I have a database structure with an one to many relationship. In the html form, there are similar inputs with a name \'item[]\' or \'file[]\' to make the data to be an array

3条回答
  •  终归单人心
    2021-01-14 22:57

    You could solve this with jQuery and add a class on every changed input
    Than you can disable all inputs without the class and submit the form

    $(document).ready(function() {
      $('input, select, textarea').on('change', function() {
        $(this).addClass('changed');
      });
      
      $('form').on('submit', function() {
        $('input:not(.changed), textarea:not(.changed)').prop('disabled', true);
        
        // alert and return just for showing
        alert($(this).serialize().replace('%5B', '[').replace('%5D', ']'));
        return false;
      });
    });
    
    
    Name:

    Item:

    File:

    Item:

    File:

    Select:

    You could use indexes in the name attribute like this

提交回复
热议问题