代码之家  ›  专栏  ›  技术社区  ›  jos

如何实现以下ajax

  •  0
  • jos  · 技术社区  · 16 年前

    <drop down box>
    ______________
    <textbox _content_ loaded via ajax onchange of drop down box>
    <some images loaded via ajax onchange of drop down box>
    

    是否有一些ajax库可用于此目的?我假设将有一个php页面来执行查询并将结果发送回主页面?

    你们能告诉我一些我应该看的例子/教程吗。

    5 回复  |  直到 16 年前
        2
  •  0
  •   Ritesh M Nayak beny23    16 年前

    你可能还想看看YUI的版本。您可以在那里创建的丰富的自动完成功能也非常酷。 http://developer.yahoo.com/yui/autocomplete/

        3
  •  0
  •   Olivvv    16 年前

    对于不需要ajax的图像,只需创建一个图像标记,即可检索图像。不能使用ajax传输文件。

        4
  •  0
  •   Tony G.    16 年前

    <script type="text/javascript" src="javascript/prototype.js">
    <script type="text/javascript">
    function select_changed() {
        // get the selected value
        var parms = 'selection='+$F('selectbox');
        // request the textbox value and pop it into the textbox
        new Ajax.Updater(
            'textbox',
            'http://example.com/textbox_handler.php',
            {
                method: 'post',
                parameters: parms
            });
    
        // request the image(s) and pop them into the image container div
        new Ajax.Updater(
            'image_container',
            'http://example.com/images_handler.php',
            {
                method: 'post',
                parameters: parms
            });
    }
    
    </script>
    
    <select id="select" onChange="javascript:select_changed();">
        <option value="1">First Value</option>
        <option value="2">Second Value</option>
    </select>
    
    <textarea name="textbox"></textarea>
    
    <div id="image_container"></div>
    
        5
  •  0
  •   Pim Jager    16 年前

    最简单的方法是使用 Jquery . 例如,你可以这样做。

    $('#dropdown').change(function(){ //listen for the change event of #dropdown box
        $.get('example.com/script.php', 
            {$('#dropdown').val()}, //send the value of dropdown as GET parameter 'val'
            function(data){
            $('#content').html(data.content);
            //first clear the image area:
            $('#imgs').empty();
            //Insert all the images
            for(x in data.imgs){
                $('#imgs').append('<img src="'+data.imgs[x]+'">');
            }
        }, 'json');
    });
    

    <select id='dropdown'>
        <option value='1'>Option1</option>
        <option value='2'>Option2 etc.</option>
    </select>
    
    <div id='content'>I contain the content, I might as well be a textarea or something else, as long is my id='content'</div>
    <div id='imgs'><img src='iContainImgs.jpg'></div>
    

    然后在服务器端创建具有以下结构的Json对象:

    content: "all the content",
    imgs: array('img1.jpg', 'img2.jpg', 'etc')