如何在单击事件javaScript之后隐藏div

时间:2014-01-05 15:25:59

标签: javascript jquery

这是我的代码(JavaScript)......

<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#track1').click(function() {
            $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>');
            $("#show").slideDown(2000);
        });
        $('#track2').click(function() {
            $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>');
            $("#show1").fadeIn(3000);
        });
        $('#track3').click(function() {
            $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>');
            $("#show1").fadeOut(3000);
            $("#show").slideUp(3000);
        });

    });

    function quick_confirm() {
        $('#wrap').append('<embed id="embed_player"     src="alert.mp3" autostart="true" hidden="true"></embed>');
        var agree = confirm("Are you sure you wish to continue?");
        if (agree) {
            $("#show1").fadeOut(3000);
            $("#show").slideUp(3000);
            return true;
        }
        else
            return false;
    }

    function valid() {
        var name = document.getElementById("name").value;
        var pass = document.getElementById("pass").value;
        error = '';
        if (name == "" && pass == "")
            error = "\nEnter both fields";
        else if (name == '')
            error = "Enter your name";
        else if (pass == '')
            error = "\nEnter your password";


        if (error != "") {
            document.getElementById("error").innerHTML = error + "<button id='showtime'>HIDE</button>";
            $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>');
            $("#error").slideDown(3000);
            return false;
        }
        $('#showtime').click(function() {
            $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>');
            $("#error").toggle; //**help me solve the problem..
        });
    }
</script>

<div id='error'></div>
<form method='post' action="testsound.php" onsubmit='return valid()' >
    <strong>Name</strong><input type='text' id='name' name='name'>
    <strong>password</strong><input type='password' id='pass' name='pass'>
    <input type='submit' value='Login'>
</form>

请尽快帮助我,需要隐藏div元素的帮助... 提交表单后,将显示div错误,但我无法执行 单击隐藏按钮后隐藏div,其ID为showtime ..

2 个答案:

答案 0 :(得分:3)

确保您实际使用括号调用toggle函数,如下所示:

$("#error").toggle();

或者可能改为使用hide

$("#error").hide();

另外,请注意,在valid函数中,如果您return falseclick事件绑定到#showtime元素,则会出现错误$('#showtime').click(function() { $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>'); $("#error").toggle(); // or hide() }); if (error != "") { document.getElementById("error").innerHTML = error + "<button id='showtime'>HIDE</button>"; $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>'); $("#error").slideDown(3000); return false; } 。有很多方法可以解决这个问题,具体取决于页面的组织方式(很难从这里提供的代码中分辨出来)。

  • 在你回来之前绑定偶数:

    document.ready
  • $(document).ready(function() { ... $('#showtime').click(function() { $('#wrap').append('<embed id="embed_player" src="alert.mp3" autostart="true" hidden="true"></embed>'); $("#error").toggle(); // or hide() }); } 处理程序中绑定事件:

    {{1}}

答案 1 :(得分:0)

你可以使用,

$('#error').click(function() {
    $(this).hide(3000);
});