2014-02-10 41 views
0

我想加載到兩個不同的div的ajax,但我檢查控制檯和網頁仍然把數據放到一個div,即使它是在ajax代碼中調用兩個不同的div。我想加載到兩個不同的div的ajax

<script>$(document).ready(function(){ 

    $.ajax({ 
     url: "http://developer.api.cnet.com/rest/v1.0/techProductSearch", 
     type: "get", 
     data: { viewType:"json", iod: "none", callback: "phone", partKey:"2nnae6wsj2w72yqhcwu4v7sg", partTag:"2nnae6wsj2w72yqhcwu4v7sg", query: "iphone 5", productId:"31303113" }, 
     dataType: "jsonp", 
     success: ipod 
    }); 

    }); 
    function ipod(data) { 
var count = 0; 
    console.log(data); 
$.each(data.CNETResponse.TechProducts.TechProduct, function(index,value) { 

    console.log("Name: "+data.CNETResponse.TechProducts.TechProduct[count].Name.$); 
    var datastring = '<tr>'+'<td class="searchItem">'+data.CNETResponse.TechProducts.TechProduct[count].Name.$+'</td> </tr>'; 
    $('#tabs-1').append(datastring); 
    console.log("$('#tabs-1').append(datastring)"); 

    count ++; 
    }); 
     }</script> 
     <script>$(document).ready(function(){ 

    $.ajax({ 
     url: "http://developer.api.cnet.com/rest/v1.0/techProductSearch", 
     type: "get", 
     data: { viewType:"json", iod: "none", callback: "phone", partKey:"2nnae6wsj2w72yqhcwu4v7sg", partTag:"2nnae6wsj2w72yqhcwu4v7sg", query: "Samsung Note 3", productId:"31303113" }, 
     dataType: "jsonp", 
     success: ipod 
    }); 

    }); 
    function ipod(data) { 
var count = 0; 
console.log(data); 
$.each(data.CNETResponse.TechProducts.TechProduct, function(index,value) { 

    console.log("Name: "+data.CNETResponse.TechProducts.TechProduct[count].Name.$); 
    var datastring = '<tr>'+'<td class="searchItem">'+data.CNETResponse.TechProducts.TechProduct[count].Name.$+'</td> </tr>'; 
    $('#tabs-2').append(datastring); 
    console.log("$('#tabs-2').append(datastring)"); 
    count ++; 
    }); 
    }</script> 

HTML

<div id="tabs-1"> 
<p></p> 
</div> 
<div id="tabs-2"> 
<p></p> 
</div> 
+1

其中之一,你有2個具有相同名稱的功能。嘗試鞏固你的代碼.. – kingkode

+0

McMastermind說你有2個函數具有相同的名稱,最後一個聲明將覆蓋首先聲明的那個。將它們重命名爲具有唯一名稱 –

+0

tr不是div的有效元素。 – epascarello

回答

1

你有兩個功能都被稱爲iPod的。你需要給他們一個獨特的名字,否則一個會覆蓋另一個。調用第二個'ipod2',然後設置第二個ajax調用以使用ipod2作爲成功回調。

0
<script> 

    $(document).ready(function(){ 

     $.ajax({ 
      url: "http://developer.api.cnet.com/rest/v1.0/techProductSearch", 
      type: "get", 
      data: { viewType:"json", iod: "none", callback: "phone", partKey:"2nnae6wsj2w72yqhcwu4v7sg", partTag:"2nnae6wsj2w72yqhcwu4v7sg", query: "iphone 5", productId:"31303113" }, 
      dataType: "jsonp", 
      success: ipod 
     }); 

    }); 

    function ipod(data) { 

     $.each(data.CNETResponse.TechProducts.TechProduct, function(i,v) { 

      var datastring = '<div class="searchItem">'+v[i].Name + '</div>'; 
      $('#tabs-'+ i).append(datastring); 

     }); 
    }; 

</script> 
相關問題