jquery show and hide image not working

六月ゝ 毕业季﹏ 提交于 2019-12-25 03:38:17

问题


I have a code to one of my pages. It's supposed to fade in, stay and fade out an image when someone clicks a radio button. Also the database is to be updated onclick using ajax.

<html>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript">
$("#rad").click(function() {
    $("#success").fadeIn(500);
    $("#success").delay(1000);
    $("#success").fadeOut(1000);
})
</script>

<table class="profimg">
    <tr><td height="50" width="50"><img id="success" src="http://cdn4.iconfinder.com/data/icons/simplicio/128x128/notification_done.png" style="display:none" height="50" width="50"></td></tr>
    <tr><td align="center"><img class="profimg" src="" alt="Administratorasdf" height="100" width="100"/></td></tr>
<tr><td id="rad" align="center"><input type='radio' title='Publicly Visible' name='img_pub' onclick="upimg1()" /> <input type='radio' title='Visible Only To Users' value='UsersOnly' name='img_pub' onclick="upimg2()" /> <input type='radio' title='Visible Only To You' value='Hide' name='img_pub' onclick="upimg3()" checked='checked'/></td></tr>
</table>

</body>
</html>

I removed the ajax part for avoid confusion. This is the jsFiddle I used. http://jsfiddle.net/c7ajb/1/ What might be the error?


回答1:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function(e) {
            $(".radios").click(function() {
                $("#success").fadeIn(500).delay(1000).fadeOut(1000);
            })
        });
        function upimg1(){}
        function upimg2(){}
        function upimg3(){}
    </script>
</head>
<body>

    <table class="profimg">
        <tr>
            <td height="50" width="50">
                <img id="success" src="http://cdn4.iconfinder.com/data/icons/simplicio/128x128/notification_done.png" style="display:none" height="50" width="50">
            </td>
        </tr>
        <tr>
            <td align="center">
                <img class="profimg" src="" alt="Administratorasdf" height="100" width="100"/>
            </td></tr>
        <tr>
            <td id="rad" align="center">
                <input class="radios" type='radio' title='Publicly Visible' name='img_pub' onClick="upimg1()" /> 
                <input class="radios" type='radio' title='Visible Only To Users' value='UsersOnly' name='img_pub' onClick="upimg2()" /> 
                <input class="radios" type='radio' title='Visible Only To You' value='Hide' name='img_pub' onClick="upimg3()" checked='checked'/>
            </td>
        </tr>
    </table>

</body>




回答2:


This is working :

$("#rad").click(function () {
    $("#success").fadeIn(500).delay(1000).fadeOut(1000);
});​

Javascript can sometime be asynchronous, if you don't do it this way, delay() will not work.

You should also use callback for a better code evolution.




回答3:


Put the delay/fadeOut in the callback of the fadeIn function (or chain the methods as suggested in the other answer):

$("#rad").click(function () {
    $("#success").fadeIn(500, function() {
        $(this).delay(1000).fadeOut(1000);
    });
});

Demo here



来源:https://stackoverflow.com/questions/13358003/jquery-show-and-hide-image-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!