这是我原来的功能。它工作得很好,我只想压缩一点:
<script type='text/javascript'>
function searchmusic(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try{
ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e){
alert('Your browser broke!');
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('searchresults');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var search = document.getElementById('search').value;
var params = 'search=' + search;
ajaxRequest.open('POST', 'getsearch.php', true);
ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajaxRequest.send(params);
}
</script>
我正在尝试稍微压缩这个函数,所以我尝试使用 jQuery 复制它:
<script>
function searchmusic(){
var search = document.getElementById('search').value;
$.ajax({
type: 'POST',
url: getsearch.php,
async: true,
data: 'search=' + search,
success: function(data) {
$('#searchresults').load(data);
}
});
}
</script>
但是,我无法使此功能正常工作。有人可以告诉我我在哪里犯了错误,为什么这个功能不起作用,或者可能是这个新功能不能像我原来的那样起作用。我不知道,但任何帮助将不胜感激,谢谢!