如何通过我jQuery和AJAX替换图像(DIV中) [英] how can i replace an image (inside a div) through jquery and ajax

查看:215
本文介绍了如何通过我jQuery和AJAX替换图像(DIV中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图点击图片,并通过jQuery的将其更改为另一个图像。

当我一步通过以下code,在服务器端,控制器动作火灾和在客户端,我可以看到萤火虫在监视窗口中正确的HTML回报,但图像不会有任何变化。任何想法,为什么这个DIV没有更新?

DIV原创:

 < D​​IV CLASS =inlineDiv切换=关闭ID =22>< IMG SRC =../../图像/票favorite- off1.pngBORDER =0>< / DIV>

jQuery的code:

  $(文件)。就绪(函数(){
        $('div.inlineDiv')。生活('点击',功能(){        VAR ID = $(本).attr(ID);
        无功切换= $(本).attr(切换);        VAR URL ='?/跟踪/更新ID ='+ ID +'和;切换='+切换;        $获得(URL,功能(数据){
            $(本)。html的(数据);
        });
    });
});

控制器动作:

 公众的ActionResult更新(INT ID,字符串切换)
  {
       如果(触发==关)
        {
            返回内容(< IMG SRC ='.. / .. /图像/票喜爱-on1.pngBORDER = 0'>中);
        }
        返回内容(< IMG SRC ='.. / .. /图像/票喜爱-off1.pngBORDER = 0'>中);
  }


解决方案

我相信这个中的get是指获取的功能。

简单:

  $(本).load(URL);

I am trying to click on an image and change it to another image through jquery.

When i step through the below code, on the serverside, the controller action fires and on the client side, i can see the correct html return in firebug watch window but the image doesn't change at all. any idea why this div is not updating?

original div:

<div class="inlineDiv" toggle="off" id="22"><img src="../../images/vote-favorite-off1.png" border="0"></div>

jquery code:

    $(document).ready(function() {
        $('div.inlineDiv').live('click', function() {

        var id = $(this).attr("id");
        var toggle = $(this).attr("toggle");

        var url = '/Tracker/Update?id=' + id + '&toggle=' + toggle;

        $.get(url, function(data) {
            $(this).html(data);
        });
    });
}); 

controller action:

  public ActionResult Update(int id, string toggle)
  {
       if (toggle == "off")
        {
            return Content("<img src='../../images/vote-favorite-on1.png' border=0'>");
        }
        return Content("<img src='../../images/vote-favorite-off1.png' border=0'>");
  }

解决方案

i believe the this in the get is referring to the get's function.

easier:

$(this).load(URL);

这篇关于如何通过我jQuery和AJAX替换图像(DIV中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆