简体   繁体   English

Ajax调用后如何保留页面的旧内容?

[英]How to retain the older content of the page after ajax call?

I am using ajax loading of for searching. 我正在使用Ajax加载进行搜索。 When I type something in textbox the searched content displayed on the page, but when I cleared the textbox an it is empty now ,the older content(before search) not displayed. 当我在文本框中键入内容时,搜索到的内容显示在页面上,但是当我清除文本框时,它现在为空,则不会显示较旧的内容(搜索之前)。 What should I do. 我该怎么办。

May be you need not sent the Ajax call when the search textbox is empty. 搜索文本框为空时,可能不需要发送Ajax调用。

You can validate if the textbox string length is greater than 0 to fire the Ajax call to fetch the search result 您可以验证文本框字符串的长度是否大于0以触发Ajax调用以获取搜索结果

<input type = "text" id = "searchbox" />

<div id = "searchResult"></div>

Script: 脚本:

var searchText = document.getElementById("searchbox").value.trim();

if (searchText.length > 0)
{
  // call Ajax to fetch search result
}

One solution is to make the searching query at the server return all the data back if the search criteria is empty. 一种解决方案是,如果搜索条件为空,则使服务器上的搜索查询返回所有数据。 In this way you can get the content if you empty the search box. 这样,如果您清空搜索框,则可以获得内容。

Make sure ajax fires even if the search box is empty. 即使搜索框为空,也请确保ajax触发。

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

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