join 2 same almost same function into 1

function showRole(str,x)
        {
            if (str=="")
              {
              document.getElementById("txtHintrole"+x+"").innerHTML="";
              return;
              } 
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("txtHintrole"+x+"").innerHTML=xmlhttp.responseText;
                }
              }

            xmlhttp.open("GET","http://localhost/tes/index.php/form/role/"+str,true);
            xmlhttp.send();
        }
function showUser(str,x)
        {
            if (str=="")
              {
              document.getElementById("txtHint"+x+"").innerHTML="";
              return;
              } 
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("txtHint"+x+"").innerHTML=xmlhttp.responseText;
                }
              }

            xmlhttp.open("GET","http://localhost/tes/index.php/form/hint/"+str,true);
            xmlhttp.send();
        }

can i join this 2 function into 1 because i need both of them to show data and i dont know how to set 2 function with this

newcell.childNodes[0].setAttribute("onchange","showUser(this.value,"+xx+");");


I'm guessing you want to call two functions on onchange . Try this:

newcell.childNodes[0].setAttribute("onchange",function(){
    showUser(this.value,"+xx+");
    secondFunction();
});

or since you tagged this jQuery, do it the jQuery way:

$(newcell.childNodes[0]).change(function(){
    showUser(this.value,"+xx+");
    secondFunction();
});
链接地址: http://www.djcxy.com/p/22326.html

上一篇: 将2个函数javascript合并为1

下一篇: 将2个几乎相同的函数加入1