繁体   English   中英

显示 ajax 从 select 选择的选项

[英]Show option chosen by ajax from a select

您好,我正在尝试从 select 中选择一个选项,以向我展示 ajax 在页面上选择的选项,但如果您能帮助我,代码不起作用谢谢

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0 /jquery.min.js"></script>
    <script>
        function fetch_select(val){
            $.ajax({
                type: 'post',
                url: 'ajax.php',
                datatype:'json',
                data: {option:val},
                success: function (response) {
                    $('#print-ajax').html(response);//This will print you result
                }
            });
        }
    </script>

  <div id="select_box">
        <select onchange="fetch_select(this.value);">
            <option value="10">state</option>
            <option value="20">20</option>
        </select>
    </div>
    <p id="print-ajax"></p><!--Result will print here-->

ajax.php

echo $_POST['option'];

有时,当您将 jQuery 文件放在 Body 标记的顶部时,它无法正常工作。
目前你的代码是完美的。 但我的建议是试试这个。

  <div id="select_box">
    <select onchange="fetch_select(this.value);">
        <option value="10">state</option>
        <option value="20">20</option>
    </select>
   </div>
<p id="print-ajax"></p><!--Result will print here-->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0 /jquery.min.js"> 
 </script>
 <script>
    function fetch_select(val){
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            dataType:'JSON',
            data: {option:val},
            success: function (response) {
                $('#print-ajax').html(response);//This will print you result
            }
        });
    }
 </script>

有时“发布”小格式会产生问题。 所以总是使用“POST”作为大写格式。 希望它对你有用。

暂无
暂无

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

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