简体   繁体   中英

Html form with automatic submit

I have a problem with html forms, I searched for a solutions on google but I didn't find any help. I want to do a html form without submit button, like here (search for activities:) , when I select option it redirect me to the value of option selected

This is a javascript question. Using jQuery, you would do it something like this:

$(document).ready(function($){
    $("select").on("change",function(){
        window.location.href = "http://example.com/"+$(this).val();
    });
});

Bind a change event handler to your select element and call the submit() method of the form when it is triggered.

Then have your server side form handler redirect to the page you want.

<form action="redirector">
    <select name="destination">
        <option value="1">Some page</option>
        <option value="2">Some other page</option>
    </select>
    <!-- Normal submit button for when the JS fails -->
    <input type="submit" value="Go">
</form>
<script>
document.querySelector('select').addEventListener('change', submitForm);
function submitForm(event) {
    event.target.form.submit();
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM