weixin_33670713 2014-12-15 14:23 采纳率: 0%
浏览 114

在提交时重置div内容

I have an isbn search input and a submit button. On form submit the results are displayed in a div below the form, works fine.

My issue is that I am able to repeatedly press submit and multiple divs appear one after another.

How can I replace the contents of the div each time submit is pressed?

My code so far;

$( "#form" ).submit(function() {

    var isbn = $('#isbn_search').val(); 
    var url='https://www.googleapis.com/books/v1/volumes?q=isbn:'+isbn;
    event.preventDefault();
    $.getJSON(url,function(data){
            $.each(data.items, function(entryIndex, entry){                   
                var html = '<div class="result">';   
                html += '<h3>' + ( entry.volumeInfo.title )+ '</h3>';  
                $(html).hide().appendTo("#result").fadeIn(1000);
            });
    });

});

<form id="form" method="post" action="">
    <label>ISBN: </label>
    <input type="text" id="isbn_search" name="isbn_search" />

    <input type="submit" value='Search Google Books' />
</form>

<div id="result"></div>
  • 写回答

2条回答 默认 最新

  • 关注

    Just reset the #result's html before adding the new data. Such as this:

    ...
    $.getJSON(url,function(data){
        $('#result').html('');
        $.each(data.items, function(entryIndex, entry){
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败