Javascript select onchange='this.form.submit()'

前端 未结 6 1088
忘了有多久
忘了有多久 2020-12-01 05:42

I have a form with a select and a few text inputs. I\'d like the form to be submitted when the select is changed. This works fine using the following:

onchan         


        
相关标签:
6条回答
  • 2020-12-01 05:46

    You should be able to use something similar to:

    $('#selectElementId').change(
        function(){
             $(this).closest('form').trigger('submit');
             /* or:
             $('#formElementId').trigger('submit');
                or:
             $('#formElementId').submit();
             */
        });
    
    0 讨论(0)
  • 2020-12-01 05:48

    Use :

    <select onchange="myFunction()">
    
        function myFunction() {
            document.querySelectorAll("input[type=submit]")[0].click();
        }
    
    0 讨论(0)
  • 2020-12-01 05:56

    My psychic debugging skills tell me that your submit button is named submit.
    Therefore, form.submit refers to the button rather than the method.

    Rename the button to something else so that form.submit refers to the method again.

    0 讨论(0)
  • 2020-12-01 06:01

    Bind them using jQuery and make jQuery handle it: http://jsfiddle.net/ZmxpW/.

    $('select').change(function() {
        $(this).parents('form').submit();
    });
    
    0 讨论(0)
  • 2020-12-01 06:07

    If you're using jQuery, it's as simple as this:

     $('#mySelect').change(function()
     {
         $('#myForm').submit();
     });
    
    0 讨论(0)
  • 2020-12-01 06:09

    There are a few ways this can be completed.

    Elements know which form they belong to, so you don't need to wrap this in jquery, you can just call this.form which returns the form element. Then you can call submit() on a form element to submit it.

      $('select').on('change', function(e){
        this.form.submit()
      });
    

    documentation: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

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