How to submit a form on change?

2019-08-06 00:37发布

问题:

The question is specifically about the Captive Network Assistant.

I have tried using vanilla JavaScript,

<form action="">
    <select name="test" onchange="this.form.submit()">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
</form>

As well using jQuery, in case there was a browser incompatibility issue.

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

However, none of them work and Captive Portal issues are not an easy cookie to debug. Is there a known solution?

回答1:

Your code looks fine. You probably want to fill in the action attribute of your form and possibly use the method and enctype attributes as well.

Here is a working copy of your code on jsFiddle



回答2:

As i remember submit() method doesn't work properly in jQuery.

Try using JavaScript: document.getElementById('ID').submit();

I had similar problem but this solved everything, so hole it helps You.