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) {
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵