简体   繁体   English

jQuery slideDown()无法正常工作

[英]Jquery slideDown() not working correctly

I have a form with 3 sections to it. 我有一个包含3个部分的表格。 At the beginning of the form, the latter two sections are hidden using css. 在表单的开头,后两个部分使用css隐藏。 Once the user presses the next button in the first section, the second section should slide down into view, however currently the animation to slide down begins and then resets immediately. 用户按下第一部分中的下一个按钮后,第二部分应向下滑动到视图中,但是当前向下滑动的动画开始,然后立即重置。 Does anyone know the problem? 有人知道这个问题吗?

Jquery script: jQuery脚本:

<script> 
        $(document).ready(function(){
            $("#buttonToSecondaryDetailsSection").click(function(){
                $("#secondaryDetailsForm").slideDown("slow");
            });

            $("#buttonToCommentsSection").click(function(){
                $("#commentsDetailsForm").slideDown("slow");
            });
        });
</script>

CSS in question: 有问题的CSS:

#secondaryDetailsForm, #commentsDetailsForm {
display: none;
}

Edit: I've tried using .show() instead of slideDown() before and the section just flashes before disappearing again. 编辑:我尝试使用.show()而不是slideDown()之前,该部分只是闪烁,然后再次消失。

Edit 2: e.preventDefault(); 编辑2:e.preventDefault(); did the trick. 做到了。 Sorry for the messy question, tried to get an example working with jsfiddle but it was throwing errors left right and center. 很抱歉出现这个混乱的问题,试图给出一个使用jsfiddle的示例,但是它向左和向右抛出错误。

This may be a solution to your issue. 这可以解决您的问题。 Thinking of your description I suppose that your buttons are doing more than they should and what actually happens is that the page is refreshing to the original state. 考虑到您的描述,我想您的按钮的作用超出了应有的程度,实际发生的是页面正在刷新到原始状态。 Can you try this? 你可以试试这个吗?

    $(document).ready(function(){
        $("#buttonToSecondaryDetailsSection").click(function(e){
            e.preventDefault();
            $("#secondaryDetailsForm").slideDown("slow");
        });

        $("#buttonToCommentsSection").click(function(e){
            e.preventDefault();
            $("#commentsDetailsForm").slideDown("slow");
        });
    });

The difference with your code is taking e as the event parameter and later stop any more actions happening with the buttons. 与您的代码的不同之处在于,将e作为事件参数,然后停止使用按钮进行的​​任何其他操作。 If my description of the problem is what actually happens then it should work with this change. 如果我对问题的描述是实际发生的情况,那么它应该适用于此更改。

I guess after the effect that you add to the component, it takes the css attribute you gave him before. 我猜想在添加到组件的效果之后,它将采用您之前给他的css属性。

<script> 
        $(document).ready(function(){
            $("#buttonToSecondaryDetailsSection").click(function(){
                $("#secondaryDetailsForm").slideDown("slow");
                $("#secondaryDetailsForm").css("display","block");
            });

            $("#buttonToCommentsSection").click(function(){
                $("#commentsDetailsForm").slideDown("slow");
                $("#secondaryDetailsForm").css("display","block");
            });
        });
</script>

Dut to the lack of code you gave us I cannot make a jsFiddle but try this please. 由于缺乏您提供给我们的代码,我无法制作jsFiddle,但请尝试一下。

use this code: it is working at my end there's no change in script (same jquery code that you are using). 使用此代码:在我的工作端,脚本没有任何变化(您使用的是相同的jquery代码)。 check your HTML. 检查您的HTML。 and try using e.preventDefault(); 并尝试使用e.preventDefault(); as mentioned by " thanpa " above 正如上面的“ thanpa ”所提到的

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<style>
#secondaryDetailsForm, #commentsDetailsForm {
display: none;
height:200px;
}
#section1{
height:200px;
}
</style>
<div id = 'section1'>
<p>Section 1</p>
<button id ='buttonToSecondaryDetailsSection'> click me </button>
</div>
<div id = 'secondaryDetailsForm'>

<p>Section 2</p>
<button id ='buttonToCommentsSection'> click me </button>
</div>
<div id = 'commentsDetailsForm'>

<p>Section 3</p>
</div>

<script>
      $(document).ready(function(){
            $("#buttonToSecondaryDetailsSection").click(function(){
                $("#secondaryDetailsForm").slideDown("slow");
            });

            $("#buttonToCommentsSection").click(function(){
                $("#commentsDetailsForm").slideDown("slow");
            });
        });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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