1

I use a captcha in my login form and I want to refresh the captcha when I click the captcha itself!but it doesn't work! my html code:

<cite class="fr">
    <img id="captcha_img" src="securimage/securimage_show.php" alt="点击图片刷新验证码"  />
</cite>

my jquery code:

$('#captcha_img').click(function(){
    //alert("hh");
    $('#captcha_img').attr("src","securimage/securimage_show.php"); 
});

I also search some docs about this question and try some other ways,but it still doesn't work,can someone give me some ideas?

8
  • does that alert show when clicked?(if uncommented?) Commented Jan 4, 2014 at 15:19
  • yes,It can alert “hh” Commented Jan 4, 2014 at 15:21
  • can you post securimage/securimage_show.php code? Commented Jan 4, 2014 at 15:22
  • i use a captcha that download from the internet,its link:phpcaptcha.org Commented Jan 4, 2014 at 15:26
  • Your code is alright. It should work. Test here: jsfiddle.net/5Y9mG/1 Commented Jan 4, 2014 at 15:26

4 Answers 4

1

Both methods below should work ( uncomment one by one and try it ):

jQuery(function ($) {

    $('#captcha_img').on( 'click', function() {
        // $(this).attr( "src","securimage/securimage_show.php?"+Math.random() ); 
        // $(this).attr( "src","securimage/securimage_show.php?"+new Date().getTime() );
    });

});
Sign up to request clarification or add additional context in comments.

Comments

1

Browser is probably caching image. Try this:

$('#captcha_img').click(function() {
    var imageUrl = 'securimage/securimage_show.php?' + new Date().getTime();
    $(this).attr('src', imageUrl); 
});

2 Comments

yeah,when I click the image,it can refresh,but it doesn't display the image,instead it displays the "alt" attr of the <img>
I am so sorry,this can also work exactle,but I lost the "?" at the end of ".php"
0

DEMO

$(function () {
    $('#foo').click(swapImages);
    var secondImg = 'http://digital-photography-school.com/wp-content/uploads/2013/03/Acorn256.png';
    function swapImages() {
        var img = $("<img id='foo' src='" + secondImg + "' />");
        $(this).replaceWith(img);
        $(img).click(swapImages);
    }
});

Comments

0
$('#captcha_img').click(function(){
    //alert("hh");
    $('#captcha_img').attr("src","securimage/securimage_show.php"); 
     return false;
});

Or use e.preventDefault() like

$('#captcha_img').click(function(e){
    e.preventDefault();
    $('#captcha_img').attr("src","securimage/securimage_show.php"); 
     //return false;
});

tell me if that works. In this case, return false will work just fine because the event doesn't need to be propagated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.