javascript 如何使网络表单在下拉选择中自动提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5341718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:53:06  来源:igfitidea点击:

How to make a web form submit itself automatically up on a dropdown selection

javascriptjquerywebformsform-submit

提问by blasteralfred Ψ

I have a form in my page like the one below;

我的页面中有一个表单,如下所示;

<form name="testform" id="testform" action="test.php" method="get">
  <input name="field1" id="field1" type="text" value="">
  <input name="field2" id="field2" type="text" value="">
  <select name="dropdown" id="dropdown">
    <option value="option1" selected="selected">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
  </select>
  <input type="submit" name="Submit" value="Submit" id="Submit">
</form>

I want the form to get submitted automatically when user select an option from the drop-down menu. How can I do this with or without using JavaScript (with or without jQuery)?

当用户从下拉菜单中选择一个选项时,我希望表单自动提交。我如何使用或不使用 JavaScript(使用或不使用 jQuery)来做到这一点?

Thanks in advance... :)

提前致谢... :)

blasteralfred

布拉拉弗雷德

回答by amit_g

Click (or select)? In that case the user would not be able to make any selection. You probably mean as soon as another option is selected. If so

单击(或选择)?在这种情况下,用户将无法进行任何选择。您可能的意思是一旦选择了另一个选项。如果是这样

<select name="dropdown" id="dropdown" onchange="this.form.submit()">

If jQueryis being used, unobtrusive event handler changeshould be used instead of inline javascript.

如果正在使用jQuery,则应使用不显眼的事件处理程序更改而不是内联 javascript。

$(function(){
    $("#dropdown").change( function(e) {
        this.form.submit();
    });
});

Use onif the form elements are dynamically being added in the DOM

如果表单元素动态添加到 DOM 中则使用on

$(function(){
    $('#testform').on( "change", "#dropdown", function(e) {
        this.form.submit();
    });
});

回答by CtrlDot

You will need to use the jQuery change() event.

您将需要使用 jQuery change() 事件。

('#dropdown').change( function() { ('#testform').submit(); })

回答by goggin13

I think

我认为

$('#dropdown').change(function () { $('Submit').click(); } );

will do the trick!

会做的伎俩!

回答by Vinoth Kumar

Here Answer

这里回答

<form name="testform" id="testform" action="test.php" method="get">
              <input name="field1" id="field1" type="text" value="">
              <input name="field2" id="field2" type="text" value="">
              <select name="dropdown" id="dropdown" onChange="document.testform.submit()">
            <option value="option1" selected="selected">option1</option>
            <option value="option2">option2</option>
            <option value="option3">option3</option>
            </select>
  </form>