drtoaamk20278 2013-05-11 14:43
浏览 65
已采纳

使用jquery ajax通过GET更新动态创建的图像

My current working situation looks something like this:

<!-- collect user info -->
<form method="GET">
    <input name="param1" type="text" />
    <input name="param2" type="text" />
    <input type="submit" />
</form>

<!-- display image based on user info -->
<img src="color.php?param1=<?php echo $_GET['param1']; ?>param2=<?php echo $_GET['param2']; ?>" alt="" />

This is working perfectly. Now I would like only the image being updated by ajax, while the rest of the HTML remains unchanged. I tried this:

<form method="GET" id="formId">
    <input name="param1" type="text" />
    <input name="param2" type="text" />
    <input type="submit" />
</form>

<div id="result"></div>

<script type="text/javascript">
    var frm = $('#formId');
    frm.submit(function(){
        $.ajax({
            type: 'GET',
            success: function () {
                $('#result').html('<img src="color.php?' + frm.serialize() + '" />');
            }
        });
        return false;
    });
</script>

In fact this solution works more or less. However, I have some issues here:

  1. Is this good jquery / ajax code or is it silly (yes, I am new to jquery)?
  2. The field "result" does update only if one of the fields changes. Can I make it update whenever the submit button is being klicked?
  3. I would like to display a spinning load.gif while the color.php calculates the picture. I tried it this way, without success:
var ajax_load = "<img class='loading' src='img/load.gif' alt='loading...' />";
$('#result').html(ajax_load).html('<img src="color.php?' + frm.serialize() + '" />');

Thank you!

  • 写回答

1条回答 默认 最新

  • douyuan4825 2013-05-11 15:01
    关注

    To the jQuery ajax call add the parameter cache : false, to force to refresh the image. To use the loader, just play with the attributo src. Copy / paste the code:

    <form method="GET" id="formId">
        <input name="param1" type="text" />
        <input name="param2" type="text" />
        <input type="submit" />
    </form>
    
    <div id="result">
         <img id="preview" src="img/load.gif" />
    </div>
    
    <script type="text/javascript">
        var frm = $('#formId');
        frm.submit(function(){
            $('#preview').attr('src', 'img/load.gif' );
            $('#preview').attr('src', 'color.php?' + frm.serialize() + '&_' + new Date().getTime() );
            return false;
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据