简体   繁体   English

带有自动提交功能的 HTML 表单

[英]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.我的 html 表单有问题,我在 google 上搜索了解决方案,但没有找到任何帮助。 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我想做一个没有提交按钮的 html 表单,就像这里(搜索活动:),当我选择选项时,它会将我重定向到所选选项的值

This is a javascript question.这是一个javascript问题。 Using jQuery, you would do it something like this:使用 jQuery,你可以这样做:

$(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.将更改事件处理程序绑定到您的选择元素,并在触发时调用表单的submit()方法。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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