简体   繁体   English

需要帮助Ajax / Javascript

[英]Need help Ajax / Javascript

I'm stumped on this one. 我对此很困惑。 I am going to have an unknown number of hidden and unhidden (depending the amount of information the user has in my database) with an Onclick function. 我将通过Onclick函数获得未知数量的隐藏和未隐藏(取决于用户在我的数据库中拥有的信息量)。 when each of the unhidden div's are clicked i pass the value of them through AJAX and grab some text from a php file. 当每个未隐藏的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 的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 -->

I need the response text from each div clicked, to replace the hidden div right underneath it. 我需要单击每个div的响应文本,以替换其下方的隐藏div。 I am having trouble passing the (index) to the getHttpRequestOne() function. 我在将(index)传递给getHttpRequestOne()函数时遇到麻烦。

You can always add custom properties to the native objects. 您始终可以将自定义属性添加到本机对象。 It might not be the best way. 这可能不是最好的方法。 but you can try this. 但是你可以试试看

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM