weixin_33726318 2014-11-28 09:59 采纳率: 0%
浏览 11

JavaScript淡入/淡出Div

I have a search input #submit, once an ISBN is submitted a div fades in below showing the results (from 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.

However when I input another ISBN and click #submit - nothing appears. The div is still hidden (or faded out). How can I make the div 'reset' to it's default setting after it has faded-out? 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. I have been looking at the jQuery fadeToggle() Method - is this correct?

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
  • 写回答

2条回答 默认 最新

  • weixin_33734785 2014-11-28 10:07
    关注

    Change

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

    to

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

    Don't use the same ID for each add button, because you are adding multiple buttons. So <button id="add" ...> isn't unique after the second press. The add button ID should match the selector of the click event handler. 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) {
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮