javascript 下拉菜单选择后自动发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13734218/
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-26 19:38:43  来源:igfitidea点击:

dropdown menu to auto post when selected

phpjavascripthtml

提问by crashwap

How would I make a drop down menu post automatically when an option has been chosen?

选择一个选项后,如何自动制作下拉菜单帖子?

<SELECT NAME="select_page"><?php echo $options1;?></SELECT>

What is the best way to achieve this?

实现这一目标的最佳方法是什么?

回答by Nick Rolando

You can utilize the onchangeevent for the selectelement, and code a form submit:

您可以将onchange事件用于select元素,并编写表单提交代码:

<form method="post">
    <select name="myselect" onchange="this.form.submit();">
        <option>blue</option>
        <option>red</option>
    </select>
</form>

That will automatically submit the form when the value is changed.

这将在更改值时自动提交表单。

回答by crashwap

Note that jQuery is referencing the <select>tag by its ID, so put one in the tag. Also, you can use AJAX to post your data if you wish to receive something back.

请注意,jQuery<select>通过其 ID引用标记,因此在标记中放置一个。此外,如果您希望收到回复,您可以使用 AJAX 发布您的数据。

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#Sel').change(function() {
                    var opt = $(this).val();
                    $.ajax({
                        type: "POST",
                        url: "receiving_file.php",
                        data: 'selected_opt=' + opt,
                        success:function(data){
                            alert('This was sent back: ' + data);
                        }
                    });
                });
            });
        </script>
    </head>
<body>

<select id = "Sel">
    <option value ="Song1">default value<br>
    <option value ="Song2">Break on through<br>
    <option value ="Song3">Time<br>
    <option value ="Song4">Money<br>
    <option value ="Song5">Saucerful of Secrets
</select>