简体   繁体   English

JavaScript淡入/淡出Div

[英]JavaScript Fade In/Out Div

I have a search input #submit , once an ISBN is submitted a div fades in below showing the results (from Google Books Api). 我有一个搜索输入#submit ,一旦提交了ISBN,则div会下面逐渐消失,并显示结果(来自Google Books Api)。 I then have a button 'Add to Library' #add - this successfully adds the info into my db via Ajax and fades out the div, all good so far. 然后,我有一个按钮'Add to Library' #add这已成功通过Ajax将信息添加到我的数据库中并淡出 div,到目前为止一切都很好。

However when I input another ISBN and click #submit - nothing appears. 但是,当我输入另一个ISBN并单击#submit时,没有任何显示。 The div is still hidden (or faded out). div仍处于隐藏状态(或淡出)。 How can I make the div 'reset' to it's default setting after it has faded-out? 逐渐消失后,如何将div“重置”为默认设置? I have to manually refresh the page each time I want to search. 每次要搜索时,我都必须手动刷新页面。

I am most likely doing something very silly as I am new to JS, however any help/direction is much appreciated. 由于我是JS新手,所以我很可能会做一些非常愚蠢的事情,但是非常感谢任何帮助/指导。 I have been looking at the jQuery fadeToggle() Method - is this correct? 我一直在看jQuery fadeToggle()方法 -这正确吗?

My Code 我的密码

$(document).ready(function() {
    $('#submit').click(function(ev) {
        ev.preventDefault();

        $.getJSON(url,function(data){
            $("#add").prop('disabled', false);
            $.each(data.items, function(entryIndex, entry){
                var html = '<div class="results well">';   
                html += '<h3>' + ( entry.volumeInfo.title || '' )+ '</h3>';  
                html += '<hr><button id="add" name="add">add to library</button>';
                $(html).hide().appendTo(".result").fadeIn(1000);

                $('#add').click(function(ev) {
                    ev.preventDefault();

                    $.ajax({
                        type: 'POST',
                        url: 'addIsbnScript.php',
                        data: {
                            'isbn' : isbn,
                            'title' : entry.volumeInfo.title
                        },
                        success: function () { //when form has been submitted successfully do below
                            $('.result').fadeOut(1000); //fade out the results div
                        }//end success function
                    });//end ajax
                });//end add button funct 
            });//end of each function
        });//end getJSON
    });// end submit.click
});//end

Change 更改

$('.result').fadeOut(1000);

to

$('.results').fadeOut(1000);

Don't use the same ID for each add button, because you are adding multiple buttons. 不要为每个添加按钮使用相同的ID,因为您要添加多个按钮。 So <button id="add" ...> isn't unique after the second press. 所以<button id="add" ...>在第二次按下后不是唯一的。 The add button ID should match the selector of the click event handler. 添加按钮ID应与click事件处理程序的选择器匹配。 For example: 例如:

$(document).ready(function() {
     var addID = 1;

     $('#submit').click(function(ev) {
         ev.preventDefault();

         $.getJSON(url,function(data){
             $("#add").prop('disabled', false);
             $.each(data.items, function(entryIndex, entry){
                 var html = '';   

                 addID++;
                 html += '<div class="results well">';
                 html += '<h3>' + ( entry.volumeInfo.title || '' )+ '</h3>';  
                 html += '<hr><button id="add'+addID+'" name="add">add to library</button>';
                 $(html).hide().appendTo(".result").fadeIn(1000);

                 $('#add'+addID).click(function(ev) {
...
success = function() {
    $('.result').fadeOut(1000, function() {
      $('.result').show();
    });
};

You cannot stop the animation halfway. 您无法中途停止动画。 ie. 即。 you cannot call show() till the fadeout animation completes. 在淡出动画完成之前,您无法调用show()。

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

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