今天我有点问题。我有一个我一直用于我的网站的ajax脚本,但出于效率原因,我需要将其转换为jQuery。我一直在研究如何做到这一点,并且没有这样的运气。
这是我的剧本:
<script type='text/javascript'>
//<![CDATA[
function ajaxFunction2(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try{
ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e){
// Something went wrong
alert('Your browser broke!');
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('editpagecolors');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var bandname = document.getElementById('bandname').value;
var musicstyle = document.getElementById('musicstyle').value;
var websiteurl = document.getElementById('websiteurl').value;
var aboutme = document.getElementById('aboutme').value;
var chooser = document.getElementById('chooser').value;
var chooser2 = document.getElementById('chooser2').value;
var chooser3 = document.getElementById('chooser3').value;
var chooser5 = document.getElementById('chooser5').value;
var chooser6 = document.getElementById('chooser6').value;
var chooser7 = document.getElementById('chooser7').value;
var params = 'bandname=' + bandname + '&musicstyle=' + musicstyle + '&websiteurl=' + websiteurl + '&aboutme=' + aboutme + '&chooser=' + chooser + '&chooser2=' + chooser2 + '&chooser3=' + chooser3 + '&chooser5=' + chooser5 + '&chooser6=' + chooser6 + '&chooser7=' + chooser7;
ajaxRequest.open("POST", 'ajaxeditprofile.php', true);
ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajaxRequest.send(params);
}
//]]>
</script>
甚至可以将其转换为jQuery,如果是这样,我将如何做到这一点。非常感谢你们的帮助!
答案 0 :(得分:1)
function ajaxFunction2(){
var bandname = $('#bandname').val();
var musicstyle = $('#musicstyle').val();
var websiteurl = $('#websiteurl').val();
var aboutme = $('#aboutme').val();
var chooser = $('#chooser').val();
var chooser2 = $('#chooser2').val();
var chooser3 = $('#chooser3').val();
var chooser5 = $('#chooser5').val();
var chooser6 = $('#chooser6').val();
var chooser7 = $('#chooser7').val();
var params = 'bandname=' + bandname + '&musicstyle=' + musicstyle + '&websiteurl=' + websiteurl + '&aboutme=' + aboutme + '&chooser=' + chooser + '&chooser2=' + chooser2 + '&chooser3=' + chooser3 + '&chooser5=' + chooser5 + '&chooser6=' + chooser6 + '&chooser7=' + chooser7;
$.post('ajaxeditprofile.php', params, function( data){
$('#editpagecolors').html( data);
});
}
答案 1 :(得分:0)
$.ajax({
url: 'yourUrl',
data: 'yourserializeddata',
type: 'POST'
}).success(function(responseHtml) {
$('#editpagecolors').html(responseHtml);
});
答案 2 :(得分:0)
查看这里的指南at netplus
以上不起作用吗?它不是非常有效但是将它包装在jquery中并且加载jquery库也不是那么高效,我也怀疑将其移植到jquery将会带来很多节省(如jquery最终会在内部调用上面的内容)。