最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a web form submit itself automatically up on a dropdown selection - Stack Overflow

programmeradmin5浏览0评论

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)?

Thanks in advance... :)

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)?

Thanks in advance... :)

blasteralfred

Share Improve this question edited Jul 16, 2012 at 14:49 Alfred asked Mar 17, 2011 at 16:02 AlfredAlfred 21.4k63 gold badges174 silver badges257 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

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 jQuery is being used, unobtrusive event handler change should be used instead of inline javascript.

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

Use on if the form elements are dynamically being added in the DOM

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

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

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

I think

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

will do the trick!

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>
发布评论

评论列表(0)

  1. 暂无评论