用jquery更改图像src

时间:2012-08-10 07:42:38

标签: c# javascript jquery asp.net-mvc asp.net-mvc-3

我有这段代码:

function EnableiPad(event) {
    var id = $(event.currentTarget).attr("data-id");
    var status = $(event.currentTarget).attr("data-status");    
    $.post("/Ipad/ChangeStatus", { id: id, enable: status }, onAjaxSuccess);
};
function onAjaxSuccess(data) {
    var status = $(".button-enable").attr("data-status");

    if (status == 'true') 
    {
        $('#statusImg').attr("src", '/Content/img/disable.png');
    }
    else 
    {
        $('#statusImg').attr("src", '/Content/img/enable.png');
    }
}

但它不会改变图像 - $('#statusImg').attr("src", '/Content/img/disable.png');

更新

这是我的图片:

@if (iPad.Enabled)
{
    <button  class="button-enable" data-original-title="Show timetable"  data-id="@iPad.Id" data-status="false" class="btn btn-mini" style="border: 0; background: transparent; box-shadow: none; padding: 0;">
        <img id="status" width="24" height="24" alt="Delete doctor" src="/Content/img/enable.png" />
    </button>
}
else
{ 
    <button id="statusDisable" class="button-enable" data-original-title="Show timetable"  data-id="@iPad.Id" data-status="true" class="btn btn-mini" style="border: 0; background: transparent; box-shadow: none; padding: 0;">
        <img id="status" width="24" height="24" alt="Delete doctor" src="/Content/img/disable.png" />
    </button>
}

1 个答案:

答案 0 :(得分:5)

你的img标签的

id 是状态,所以它应该是:

$('#status').attr("src", '/Content/img/disable.png');