weixin_33696106
weixin_33696106
2019-12-28 08:02
采纳率: 0%
浏览 34

jQuery不需要的selectedIndex

 function getrecipe(q) {
  var apiKey = "";
  $.ajax({
    url: "https://api.spoonacular.com/recipes/search",
    data: "apiKey=" + apiKey + "&query=" + q,
    async: 'true',
    success: GetReci
  });

  function GetReci(response) {
    // console.log(response)
    var reci = "";
    var i = 0;
    var GetRecipe_Arr = response.results;
    $.each(GetRecipe_Arr, function(index,value){
      var title = GetRecipe_Arr[i].title;

      i = i + 1;
    })

    $('#recipeoutput').html(reci);

    $('#recipeoutput li').on('tap', function (e) {
      recipeIndex = $(this).index();
      $(":mobile-pagecontainer").pagecontainer("change", "#searchrecipedetailpage", { role: "page" 
    });
    });

    $(document).on('pageshow', '#searchrecipedetailpage', function () {
      var id = GetRecipe_Arr[recipeIndex].id
      console.log(id)
      var title = GetRecipe_Arr[recipeIndex].title;
      $.ajax({
        url: "https://api.spoonacular.com/recipes/" + id + "/information",
        data:  "apiKey=" + apiKey + "&includeNutrition=true",
        async: 'true',
        success: function(response){
          console.log(response);
        }
      });
   })
}

The first time search the id was fine...but after the second time search, I get enter image description here extra id that is not I tapped on. Sorry for my bad explanations, how can i improve my codes and make it work well?

图片转代码服务由CSDN问答提供 功能建议

                    
 函数getrecipe(q){
   var apiKey =“”;
   $ .ajax({
     网址:“ https://api.spoonacular.com/recipes/search”,
     数据:“ apiKey =” + apiKey +“&query =” + q,
     异步:“ true”,
     成功:GetReci
   });

   函数GetReci(response){
     // console.log(响应)
     var reci =“”;
     var i = 0;
     var GetRecipe_Arr = response.results;
     $ .each(GetRecipe_Arr,function(index,value){
       var title = GetRecipe_Arr [i] .title;

       我=我+ 1;
     })

     $('#recipeoutput')。html(reci);

     $('#recipeoutput li')。on('tap',function(e){
       recipeIndex = $(this).index();
       $(“:mobile-pagecontainer”)。pagecontainer(“ change”,“ #searchrecipedetailpage”,{角色:“ page”
     });
     });

     $(document).on('pageshow','#searchrecipedetailpage',function(){
       var id = GetRecipe_Arr [recipeIndex] .id
       console.log(id)
       var title = GetRecipe_Arr [recipeIndex] .title;
       $ .ajax({
         网址:“ https://api.spoonacular.com/recipes/” + id +“ /信息”,
         数据:“ apiKey =” + apiKey +“&includeNutrition = true”,
         异步:“ true”,
         成功:功能(响应){
           console.log(response);
         }
       });
    })
 }
  

 

第一次搜索id很好...但是第二次搜索后,我得到在此处输入图片描述我没有点击的其他ID。 对不起我的不好的解释,我该如何改善我的代码并使它正常运行?     

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐