我有一个简单的 Javascript 函数,可以将异步 POST 请求发送到 2 个不同的 php 脚本。这是一个简单的代码:
<script type="text/javascript">
function send(oForm, what)
{
var ios3 = oForm.ios3.checked;
var ios4 = oForm.ios4.checked;
var ios5 = oForm.ios5.checked;
var id = oForm.id.value;
if (what == confirm)
{
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}// code for IE7+, Firefox, Chrome, Opera, Safari
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var output = xmlhttp.responseText;
if (output != ""){document.getElementById("debug").innerHTML=output; document.getElementById(id).style.display = 'none';}
}
}
xmlhttp.open("POST","ajax/confirm.php",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('id=' + encodeURIComponent(id) + '&ios3=' + encodeURIComponent(ios3) + '&ios4=' + encodeURIComponent(ios4) + '&ios5=' + encodeURIComponent(ios5));
}
if (what == remove)
{
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}// code for IE7+, Firefox, Chrome, Opera, Safari
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var output = xmlhttp.responseText;
if (output != ""){document.getElementById("debug").innerHTML=output; document.getElementById(id).style.display = 'none';}
}
}
xmlhttp.open("POST","ajax/confirm.php",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('id=' + encodeURIComponent(id) + '&ios3=' + encodeURIComponent(ios3) + '&ios4=' + encodeURIComponent(ios4) + '&ios5=' + encodeURIComponent(ios5));
}
}
我用这些简单的输入按钮调用这个函数(没有“\”我已经为php echo添加了这些)
<input type=\"button\" id=\"submit\" value=\"Confirm\" onclick=\"send(this.form, confirm)\" />
<input type=\"button\" id=\"remove\" value=\"Remove\" onclick=\"send(this.form, remove)\" />
有人能解释一下为什么它只适用于第一个按钮吗?