submit form when elements change

后端 未结 4 1997
渐次进展
渐次进展 2021-01-12 03:57

In jQuery, if I assign class=auto_submit_form to a form, it will be submitted whenever any element is changed, with the following code:

/* autom         


        
相关标签:
4条回答
  • 2021-01-12 04:44

    I would give an id to the form:

    $(".auto-submit-item").change(function() {
        $("form#auto-submit").submit();
    });
    
    0 讨论(0)
  • 2021-01-12 04:47

    I came up with a generic approach to this:

    $('.autoSubmit, .autoSubmit select, .autoSubmit input, .autoSubmit textarea').change(function () {
        const el = $(this);
        let form;
    
        if (el.is('form')) { form = el; }
        else { form = el.closest('form'); }
    
        form.submit();
    });
    

    All elements of a form:

    <form class="autoSubmit">
        <select><option>1</option><option>2</option></select>
    </form>
    

    Only individual elements

    <form>
        <select class="autoSubmit"><option>1</option><option>2</option></select>
    </form>
    
    0 讨论(0)
  • 2021-01-12 04:54

    You can use an expression in the parents() method to filter the parents. Hence this might be a little more efficient:

    /* submit if elements of class=auto_submit_item in the form changes */
    $(".auto_submit_item").change(function() {
        $(this).parents("form").submit();
    });
    
    0 讨论(0)
  • 2021-01-12 05:01
     /* submit if elements of class=auto_submit_item in the form changes */
    $(function() {
       $(".auto_submit_item").change(function() {
         $("form").submit();
       });
     });
    

    Assumes you only have one form on the page. If not, you'll need to do select the form that is an ancestor of the current element using $(this).parents("form").submit()

    0 讨论(0)
提交回复
热议问题