2013-02-28 61 views
0

我試圖用js和ASP.NET MVC3剃刀更新驗證碼。我發現這個解決方案: 查看:jquery update captcha

<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成功運行。但控制器操作不會運行。哪裏不對?有任何想法嗎?

回答

3

我認爲你的選擇器$('Captchaimg')一定是錯的。如果您使用的是ID,則應該是$('#Captchaimg')

也是什麼觸發這個動作?我會用:

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

而且我會更改id的小寫。

+0

謝謝,我的注意力會在某個時候殺死我 – Yury 2013-02-28 11:45:42