2011-06-15 123 views
0

我想補充圖像,當我點擊capthca。我怎樣才能做到這一點?asp.net mvc jquery填充圖像

public ActionResult Image() 
    { 
     var builder = new XCaptcha.ImageBuilder(); 
     var result = builder.Create(); 
     Session.Add("Cap", result.Solution); 
     return new FileContentResult(result.Image, result.ContentType); 
    } 


<script language="javascript" type="text/javascript"> 
$(document).ready(function() { 
    $('#images').click(function() { 

     $.ajax({ 
      url: "/en/form/Image", 
      success: function (mydata) { 
       $("#images").attr("src", mydata); 
      }, 
      type: "POST" 
     }); 
     return false; 
    }); 
}); 

<input type="image" id="images" src="<%= Url.Action("Image", "ContactForms") %>" 
         alt="Click to refresh" /> &nbsp; &nbsp;<input type="text" size="5" tabindex="1000" dir="ltr" maxlength="5" name="Captcha" id="Captcha" /> 

回答

2
$('#images').click(function() { 
    $(this).attr('src', function() { 
     // the datetime portion appended to the url avoids caching issues 
     // and ensures that a fresh image will be loaded every time 
     var d = new Date(); 
     return this.src + '?' + d.getTime(); 
    }); 

    // Remark: not sure about the return false here. This will cancel the 
    // default action of the image button => might not be what you need => 
    // adapt to your requirements 
    return false; 
}); 
0

儘量只src屬性設置爲動態圖像網址的網址

<script language="javascript" type="text/javascript"> 
$(document).ready(function() { 
    $('#images').click(function() { 
     $(this).attr("src", "/en/form/Image"); 
     return false; 
    }); 
});