0

これは 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']; ?>

都市を選択すると、「選択した」と表示され、次に都市の名前が表示されます。しかし、それは何もしません..

4

2 に答える 2

2

あなたはデータを送信していません...データはここでは定義されていません...コード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
            });             
        });
于 2013-03-14T14:18:18.187 に答える
1

速記を使用することもできます...しかし、ここでの本当の問題は、2つの変数が「未定義」であるということです。html変数は定義されておらず、前述のようにデータ変数も定義されていません。

これを試してみてください

   $('#city').change(function() {
        var data= "city=" + $('#city').val();
        $.ajax({
            url: 'page.php', 
            type: 'GET',
            data: data,
            success: function(html){ 
                $("#my_div").html(html); 
            }
        });             
    });
于 2013-03-14T14:24:04.367 に答える