繁体   English   中英

使用Ajax()函数的jQuery ajax调用不起作用

[英]jQuery ajax call with Ajax() function doesn't work

这是index.php的代码:

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
    <script type="text/javascript">
        jQuery(function () {    
            jQuery('#city').change(function() {
                var city = jQuery('#city').val();           
                var data = "city=" + city; 
                jQuery.ajax({
                    url: 'page.php', 
                    type: 'GET',
                    data: data,
                    success: function(data){ jQuery("#my_div").html(data); }
                });             
            });
        });
    </script>
</head>
<body>
    <select id='city'>  
        <option value='Paris'>Paris</option>
        <option value='London'>London</option>
        <option value='Rome'>Rome</option>
    </select>   
    <div id='my_div'>
        <?php require_once('page.php'); ?>      
    </div>
</body>
<html>

和page.php:

<?php if (isset($_GET['city'])) echo 'you selected '.$_GET['city']; ?>

选择的城市应显示“您选择的”,然后显示城市名称。 但是它什么也没做。

您没有发送数据...此处的数据未定义...更改代码var city = "city=" + city; var data= "city=" + city; ..以及您得到的回应都是数据..所以更换

jQuery("#my_div").html(html);

jQuery("#my_div").html(data);

尝试这个

 jQuery('#city').change(function() {
            var city = jQuery('#city').val();           
            var data= "city=" + city; //<--here
            jQuery.ajax({
                url: 'page.php', 
                type: 'GET',
                data: data,
                success: function(data){ jQuery("#my_div").html(data); } //<--here
            });             
        });

您可以使用速记...但是这里真正的问题是两个变量是“ undefined”。 没有定义html变量,也没有定义数据变量,如前所述。

试一下

   $('#city').change(function() {
        var data= "city=" + $('#city').val();
        $.ajax({
            url: 'page.php', 
            type: 'GET',
            data: data,
            success: function(html){ 
                $("#my_div").html(html); 
            }
        });             
    });

暂无
暂无

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

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