【问题标题】:Need help Ajax / Javascript需要帮助 Ajax / Javascript
【发布时间】:2012-11-30 05:39:44
【问题描述】:

我被这个难住了。我将使用 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。我无法将 (index) 传递给 getHttpRequestOne() 函数。

【问题讨论】:

    标签: javascript html ajax


    【解决方案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;    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2021-05-16
      • 2015-06-28
      • 2012-05-23
      • 1970-01-01
      相关资源
      最近更新 更多