0

私はこれに困惑しています。Onclick 関数を使用して、(ユーザーがデータベースに持っている情報の量に応じて) 非表示と非表示の数が不明になります。非表示の div のそれぞれがクリックされると、AJAX を介してそれらの値を渡し、php ファイルからテキストを取得します。

     function selectProduct(index) {
   var option = document.getElementById('sel'+index).value;
        var queryStringOne = "?option="+option;
         http.open("GET", "product.php" + 
                          queryStringOne, true);
        http.onreadystatechange = getHttpResOne+index;
          http.send(null); 
    }

    function getHttpResOne(index) {

      if (http.readyState == 4) { 
       resOne = http.responseText;  // These following lines get the response and update the page
  document.getElementById('prohidden'+index).innerHTML = resOne;    
   }
 }

HTML

 <div id="sel1" value="sel1"  onClick="selectProduct(1); return false;">Select Item</div>
    <div id="prohidden1" class="sel"></div> <!-- hidden -->
    <div id="sel2" value="sel2"  onClick="selectProduct(2); return false;">Select Item</div>
    <div id="prohidden2" class="sel"></div><!-- hidden -->

クリックした各 div からの応答テキストが必要で、そのすぐ下にある非表示の div を置き換えます。(インデックス) を getHttpRequestOne() 関数に渡すのに問題があります。

4

1 に答える 1

1

カスタム プロパティはいつでもネイティブ オブジェクトに追加できます。それは最善の方法ではないかもしれません。しかし、あなたはこれを試すことができます。

function selectProduct(index) {
    var option = document.getElementById('sel'+index).value;
    var queryStringOne = "?option="+option;
    http.open("GET", "product.php" + 
              queryStringOne, true);
    http.onreadystatechange = getHttpResOne;
    http.myCustomValue = index;
    http.send(null); 
}

function getHttpResOne() {
    if (http.readyState == 4) { 
    resOne = http.responseText;  // These following lines get the response and update the page
    var index = http.myCustomValue;
    document.getElementById('prohidden'+index).innerHTML = resOne;    
    }
}
于 2012-11-30T06:08:48.703 に答える