分离自动完成jQuery页面

问题描述:

我已经从jquery的自动完成功能。我已经测试过,除了加载打开页面太长时,它工作正常。所以我想再次创建一个文件,这个页面可以更快打开或重新加载速度更快。分离自动完成jQuery页面

这是自动完成文件

<?php 
include "../config/config.php"; 
$region = $_GET['region']; 
?> 
<script> 
$(function() { 
var availableTags = [ 
<?php 
    $sql=mysql_query("SELECT country_name FROM country WHERE region='$region' ORDER BY country_name"); 

    while($f=mysql_fetch_array($sql)) 
    { 
     echo " '".$f['country_name']."', "; 
    } 
?> 

]; 
function split(val) { 
    return val.split(/,\s*/); 
} 
function extractLast(term) { 
    return split(term).pop(); 
} 

$("#tags") 
    // don't navigate away from the field on tab when selecting an item 
    .bind("keydown", function(event) { 
    if (event.keyCode === $.ui.keyCode.TAB && 
     $(this).autocomplete("instance").menu.active) { 
     event.preventDefault(); 
    } 
    }) 
    .autocomplete({ 
    minLength: 0, 
    source: function(request, response) { 
     // delegate back to autocomplete, but extract the last term 
     response($.ui.autocomplete.filter(
     availableTags, extractLast(request.term))); 
    }, 
    focus: function() { 
     // prevent value inserted on focus 
     return false; 
    }, 
    select: function(event, ui) { 
     var terms = split(this.value); 
     // remove the current input 
     terms.pop(); 
     // add the selected item 
     terms.push(ui.item.value); 
     // add placeholder to get the comma-and-space at the end 
     terms.push(""); 
     this.value = terms.join(", "); 
     return false; 
    } 
    }); 
    }); 
</script> 
</head> 
<body> 

<div class="ui-widget"> 
<label >Type country: </label> 
<input id="tags" size="50"> 
</div> 

我如何使用AJAX和sql文件分成两个文件?

+0

我不明白,你想做什么?你想从外部文件追加脚本,在页面完全加载后运行脚本还是什么? – Sojtin

+0

@Sojtin是的,我想创建一个与jquery分开的php文件,所以我认为重载页面会更快 – Iker

把你的PHP在例如一个单独的文件名为“getRegions.php”

<?php 
    include "../config/config.php"; 
    $region = $_GET['term']; //this has to be changed to "term" !!!! 

    $sql=mysql_query("SELECT country_name FROM country WHERE region='$region' ORDER BY country_name"); 

    $regions = array();  
    while($f=mysql_fetch_array($sql)) 
    { 
     $regions[] = $f['country_name']; 
    } 

    echo json_encode($regions); 

然后,而不是在你的JavaScript部分使用var availableTags,改变.ajax()source属性source: 'getRegions.php'的。

这应该做的伎俩!