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

前端 未结 6 1114
忘了有多久
忘了有多久 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 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

提交回复
热议问题