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

在提交时重置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){
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥60 如何批量获取json的url
  • ¥15 对法兰连接元件所承受的表面载荷等效转化为法兰开孔接触面上的等效表面载荷?
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼