这是我原来的功能。它工作得很好,我只是想压缩一下:
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);
}
我正在尝试压缩此函数,所以我尝试使用jQuery复制它:
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);
}
});
}
但是,我无法使此功能正常工作。有人可以告诉我我在哪里犯了错误,以及为什么这个功能不起作用,或者可能是这个新功能不能像我原来那样起作用。我不知道,但是非常感谢任何帮助,谢谢!