简体   繁体   中英

Javascript and ASP MVC - refreshing an image

I have a page that contains a captcha. whenever the user enters the captcha incorrectly the form will submit and they will move on. If not, i want to refresh the captcha image so that the user has to enter the new number. this code currently works in Chrome, but the image wont refresh in firefox. does not work in IE either.

javascript method:

function CheckCaptcha() {
    var UserInput = $("#Captcha").val();

    $.ajax({
        type: "POST",
        url: "/Messaging/CheckUserCaptcha",
        data: { userCaptcha: UserInput },
        success: function (data) {
            //alert("SUCCESS: " + data.success);
            //alert("SUCCESS2: " + data.message);
            var correct = data.success;
            if (correct == true) {
                document.forms[0].submit();
            } else {  THIS IS WHERE I NEED TO REFRESH THE CLASS
                alert(data.message);
                $(".captchaImage").attr("src", "<%= Url.Action("CaptchaImage", "Messaging", new {prefix = "CaptchaImage"}) %>");
            }    
        }
    });
    return false;
}

this is what the HTML looks like

    <img src="/Messaging/CaptchaImage?prefix=CaptchaImage" class="captchaImage"></img>

i used the answer from this question to answer mine: How to reload/refresh an element(image) in jQuery

so essentially my question may have been a copy of his question. it just took me a while to find the answer.

i added a var d, which was instantiated as a new instance of the Date object. then i concatenated onto my image the getDate() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM