2015-03-03 146 views
6

我创建了一个应用程序,其中有一个用于从表中搜索信息的文本框。尽管当我们在搜索文本框中输入字符时,我已经编写了代码,但接受一个字符控件后,文本框不再出现。 这是我searching`从动态创建的表中搜索数据

<script type="text/javascript"> 
 
$(document).ready(function() 
 
{ 
 
    var minlength = 1; 
 

 
    $("#searchTerm").keyup(function() { 
 
     
 
     value = $(this).val(); 
 
\t \t 
 
\t \t if (value.length > minlength) 
 
\t \t { 
 
\t \t \t searchTable(value); 
 
\t \t } 
 
\t \t else if(value.length < minlength) 
 
\t \t { 
 
\t \t \t searchTable(""); 
 
\t \t } \t 
 
\t }); 
 
}); 
 
function searchTable(value) 
 
{ 
 
\t $.ajax({ 
 
\t \t type: "GET", 
 
\t \t url: "dispatient.php", 
 
\t \t data:({search_keyword: value}), 
 
\t \t success: function(success) 
 
\t \t { 
 
\t \t \t window.location.href = "dispatient.php?search_keyword="+value; \t 
 
\t \t \t $("#searchTerm").focus(); 
 
\t \t }, 
 
\t \t error: function() 
 
\t \t { 
 
\t \t \t alert("Error occured.please try again"); \t 
 
\t \t }, 
 
\t \t complete: function(complete) 
 
\t \t { 
 
\t \t \t $("#searchTerm").focus(); 
 
\t \t }, 
 
\t \t 
 
\t }); 
 
}
<input id="searchTerm" Type="text" class="search_box" placeholder="Search" 
 
\t \t \t value = <?php echo $_GET['search_keyword'] ?> >

` 请建议我代码..

在此先感谢..

+0

没点。 – EternalHour 2015-03-03 07:01:45

回答

2

value是JavaScript的默认属性尝试将变量名value更改为类似searchData

+0

谢谢@narendra即时更改该值属性,但仍然工作plzzz帮我 – 2015-03-03 06:10:53

+0

检查是否引发错误... – 2015-03-03 06:13:22

+0

没有任何错误显示.. – 2015-03-03 06:15:17

2

在您的成功回调中,您将页面重定向到dispatient.php。我相信,这是具有搜索功能的页面。一旦你重定向,页面重装,没有一点写作:

$("#searchTerm").focus(); 

因为,你已经在使用AJAX,尝试加载到通过JavaScript/jQuery的你的页面从成功中的数据,而无需重新加载页面。

+0

谢谢你的回复给我。当我输入文本框的值是,但控制超出了文本框,即页面完全重新加载。先生,请提出任何方法。 – 2015-03-03 06:07:29

+0

先生如何留在文本框中的文本 – 2015-03-03 06:47:03

+0

删除行window.location.href =“dispatient.php?search_keyword =”+ value;如果你真的想重新加载页面,那么你需要找到一种方法来重新加载页面后将焦点返回到搜索框。虽然,我没有看到这样做的重点。请不要将我称为先生,这有点尴尬。谢谢! – littleibex 2015-03-03 06:50:43

2

创建一个div并加载您的数据,而不是重新加载整个页面。

尝试使用AJAX,如果你要刷新页面,还不如使用服务器端语言这样的事情,而不是Ajax调用

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

$("#searchResult").load("search.php?search_keyword=value",function(){ 
//your callback 
});