2017-06-29 3 views
0

jsのHTMLでAJAXにid変数を取得する方法

function SearchInList(_id,_url,_place){ 
    this.id = _id; 
    this.url = _url; 
    this.place = _place; /*How to get this value */ 
}; 
SearchInList.prototype.FindMe = function (_str){ 
this.str = _str; 
    if (this.str == "") { 
     if (window.XMLHttpRequest) { 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest(); 
     } else { 
      // code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     var place = this.place; 
     xmlhttp.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       document.getElementById("lista-ind").innerHTML = this.responseText; /*in this place */ 
      } 
     }; 
     xmlhttp.open("GET",this.url+"?id="+this.id,true); 
     xmlhttp.send(); 
    } else { 
     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 (this.readyState == 4 && this.status == 200) { 
       document.getElementById("lista-ind").innerHTML = this.responseText; 
      } 
     }; 
     xmlhttp.open("GET",this.url+"?id="+this.id+"&hint="+this.str,true); 
     xmlhttp.send(); 
    } 
}; 

私は

<input type="text" placeholder="Search" id="search-ind" ></div> 
    <div id="lista-ind" class="lista"></div> 
<script> 
    var id = <?php echo $_GET['id']; ?>; 
    var url = "showresults.php"; 
    var place = "lista-ind"; 

    var searchInd = new SearchInList(id, url); 
    var searchboxInd = document.getElementById("search-ind"); 

    window.onload = searchboxInd.addEventListener("keyup", function(){ 
     searchInd.FindMe(searchboxInd.value,place); 
     console.log(searchboxInd.value); 
    }, false); 

    window.onload = searchInd.FindMe("",place); 

</script> 

を持っていると私はonreadystatechangeに "のdocument.getElementById(" リスタ-IND ")"、それに機能を持っているとき、は動作していますが、 document.getElementById(this.place)に変更するとそうではありません。

この変数をその関数に渡すにはどうすればよいですか?

これはリストの検索方法です。ありがとう。 M.

答えて

0

placeがグローバル変数の場合、関数の外にはthisは不要です。変数名だけです。

document.getElementById(place) 
0

あなたは、パラメータとして行われていない場所のないSearchInListFindMeをインスタンス化しているので、オブジェクトに使用する場所を取得する方法はありません。
最も簡単な解決策は、私がSearchInListに、このパラメータを持つことができますFindMe

SearchInList.prototype.FindMe = function (_str, _place){ 
    this.str = _str; 
    this.place = _place; 
    ... 
+0

へのパラメータとして場所を追加するだろうか? 私は1つのページに2つのリストがあり、2つのSearchInListオブジェクトを別の場所に配置する必要があります。 と私はthis.placeを持っているとき、それは動作していません。 この関数 function(){ if(this.readyState == 4 && this.status == 200){ document.getElementById( "lista-ind")。innerHTML = this.responseText;/*ここには*/ } この場所はありません(ただし、グローバルな場所を見ています) –

関連する問題