简体   繁体   中英

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

I am using ajax loading of for searching. 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.

You can validate if the textbox string length is greater than 0 to fire the Ajax call to fetch the search result

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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