jquery更新验证码

时间:2013-02-28 11:28:14

标签: jquery asp.net-mvc razor

我尝试使用js和ASP.NET MVC3 razor更新验证码。我发现这个解决方案: 视图:

<div id ="CaptchaDiv"> <img alt="Captcha" id="Captchaimg" src="@Url.Action("GetCaptcha")" style="" /></div>  <input type="button"  value="Refresh" onclick=" return GetCaptcha()" />

控制器:

public ActionResult GetCaptcha ()
        {
            FileContentResult img = null;
            int res = 0;
            MemoryStream mem = Metods.CaptchaImage( out res);
            Session["Captcha"] = res;
          img = this.File(mem.GetBuffer(), "image/Jpeg");
            return img;
        }

JS:

function GetCaptcha()
 {
     $('Captchaimg').attr('src', '/Account/GetCaptcha?' + new Date().getTime());
 }

但是:它不起作用。 JS成功运行。但是控制器动作不会运行。怎么了?有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我认为你的选择器$('Captchaimg')一定是错的。如果您使用的是ID,则应为$('#Captchaimg')

还有什么可以触发这个动作?我会用:

$('#Captchaimg').click(function(){
    $(this).attr('src', '/Account/GetCaptcha?' + new Date().getTime());
});

我会将id改为小写。