使用XMLHttpRequest的Javascript函数

使用XMLHttpRequest的Javascript函数,javascript,function,Javascript,Function,我有一个简单的Javascript函数,可以向两个不同的php脚本发送异步POST请求。下面是一个简单的代码: <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 (wh

我有一个简单的Javascript函数,可以向两个不同的php脚本发送异步POST请求。下面是一个简单的代码:

<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));
}

函数发送(oForm,what)
{
var ios3=oForm.ios3.checked;
var ios4=oForm.ios4.checked;
var ios5=oForm.ios5.checked;
var id=of form.id.value;
如果(什么==确认)
{
if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}//IE7+、Firefox、Chrome、Opera、Safari的代码
else{xmlhttp=newActivexObject(“Microsoft.xmlhttp”);}//IE6、IE5的代码
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
var输出=xmlhttp.responseText;
if(output!=“”){document.getElementById(“debug”).innerHTML=output;document.getElementById(id).style.display='none';}
}
}
open(“POST”,“ajax/confirm.php”,true);
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
send('id='+encodeURIComponent(id)+'&ios3='+encodeURIComponent(ios3)+'&ios4='+encodeURIComponent(ios4)+'&ios5='+encodeURIComponent(ios5));
}
如果(什么==删除)
{
if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}//IE7+、Firefox、Chrome、Opera、Safari的代码
else{xmlhttp=newActivexObject(“Microsoft.xmlhttp”);}//IE6、IE5的代码
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
var输出=xmlhttp.responseText;
if(output!=“”){document.getElementById(“debug”).innerHTML=output;document.getElementById(id).style.display='none';}
}
}
open(“POST”,“ajax/confirm.php”,true);
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
send('id='+encodeURIComponent(id)+'&ios3='+encodeURIComponent(ios3)+'&ios4='+encodeURIComponent(ios4)+'&ios5='+encodeURIComponent(ios5));
}
}

我用这些简单的输入按钮调用这个函数(没有“\”我为php echo添加了这些)



有人能解释一下为什么它只适用于第一个按钮吗?

'可能是因为
确认
被解释为javascript函数
确认()。它实际上返回了一些东西。我假设在这种情况下,
remove
是未定义的。尝试将字符串作为第二个参数传递,并将该字符串与另一个字符串进行比较。那就好像

<input type=\"button\" id=\"submit\" value=\"Confirm\" onclick=\"send(this.form, 'confirm')\" />
<input type=\"button\" id=\"remove\" value=\"Remove\" onclick=\"send(this.form, 'remove')\" />

在哪里定义了
确认
删除
xmlhttp=new
-全局变量是邪恶的。不要使用Globals!
<input type=\"button\" id=\"submit\" value=\"Confirm\" onclick=\"send(this.form, 'confirm')\" />
<input type=\"button\" id=\"remove\" value=\"Remove\" onclick=\"send(this.form, 'remove')\" />
if (what == "confirm")
{
   ...
}
if (what == "remove")
{
   ...
}