重复功能

时间:2014-05-06 16:02:11

标签: javascript ajax

你好,你好吗?

我需要重复此代码100次。如果您只查看更改" showuser []"和" cargarproducto []"。我可以这样做,不必写100次?

有人可以帮帮我吗?非常感谢你

function showUser1(str){
    if (str==""){
      document.getElementById("cargarproducto1").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("cargarproducto1").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","catalog/model/catalog/obtendatos.php?q="+str,true);
    xmlhttp.send();
    }

    function showUser2(str){
    if (str==""){
      document.getElementById("cargarproducto2").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("cargarproducto2").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","catalog/model/catalog/obtendatos.php?q="+str,true);
    xmlhttp.send();
    }

    function showUser3(str){
    if (str==""){
      document.getElementById("cargarproducto3").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("cargarproducto3").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","catalog/model/catalog/obtendatos.php?q="+str,true);
    xmlhttp.send();
    }

1 个答案:

答案 0 :(得分:1)

您可以为其编写通用函数传递URL,参数和目标div ID。

function makeRequest(url, str, div_id) {
if (str==""){
  document.getElementById(div_id).innerHTML="";
  return;
  }

    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            http_request.overrideMimeType('text/xml');
            // http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            }
        }
    }
    if (!http_request) {
        alert('Your browser does not support AJAX!');
        return false;
    }
    http_request.onreadystatechange = function(){
  if (http_request.readyState==4 && http_request.status==200){
    document.getElementById(div_id).innerHTML=http_request.responseText;
    }
    http_request.open('GET', url + str, true);
    http_request.send(null);
}