2014-01-24 30 views
0

我正在處理我的應用程序,並在我的html中有三個鏈接evere鏈接屬於不同類別,我想在有人點擊時加載當前類別。但問題在於我的功能,我不知道如何捕捉選定的類別。

這是我的代碼:

這是在HTML中的三個環節:

<a id="rock" data-role="button" data-transition="slide" href="category.html">Rock</a> 
    <a id="electronica" data-role="button" data-transition="slide" href="category.html">Electrónica</a>   
    <a id="pop" data-role="button" data-transition="slide" href="category.html">Pop</a> 

這是我的功能,你可以在我試圖與每個ID的東西,但沒有任何反應beggining見,其次,Ajax和數據應該捕獲類別的名稱。

var valueCat = false; 

     $('#rock, #electronica').click(function() { 
      if($(event.target).attr('id')=='rock'){ 
       valueCat = 'rock'; 
       valueCat = true; 
       return; 
      } 
      else if($(event.target).attr('id')=='pop'){ 
       valueCat = $('#pop').attr('id'); 
       valueCat = true; 
       return; 
      } 
     }); 


     //alert(valueCatRockArg); 
     $.ajax({   
      url: 'http://.....org/api/get_category_posts', 
      data:'category_name='+valueCat+'&count=5&order=desc', 
      type: 'GET', 
      dataType: 'jsonp', 
      success: function(data){    
       var source = $("#category-template").html(); //Get the HTML from the template in the script tag 
       var template = Handlebars.compile(source); // compilamos la plantilla 
       var blogData = template(data); // en data se almacena el contenido que nos viene de wordpress 
       $('#category-data').html(blogData); // mostramos el contenido 
       $('#category-data').trigger('create'); 
       dfd.resolve(data); 

      }, 

      error: function(data){ 
       console.log(data); 
      } 
     }); 

我希望你能理解我,我是新的應用程序和壞的我的英語。

謝謝你提前!

功能齊全:

category: function(){ 
    function getCategory() { 
     var dfd = $.Deferred(); 


     //alert(valueCatRockArg); 
     $.ajax({   
      url: 'http://ultravoz.org/api/get_category_posts', 
      data:'category_name='+valueCat+'&count=5&order=desc', 
      type: 'GET', 
      dataType: 'jsonp', 
      success: function(data){    
       var source = $("#category-template").html(); //Get the HTML from the template in the script tag 
       var template = Handlebars.compile(source); // compilamos la plantilla 
       var blogData = template(data); // en data se almacena el contenido que nos viene de wordpress 
       $('#category-data').html(blogData); // mostramos el contenido 
       $('#category-data').trigger('create'); 
       dfd.resolve(data); 

      }, 

      error: function(data){ 
       console.log(data); 
      } 
     }); 

     return dfd.promise(); 
    }; 

    getCategory().then(function(data){ 
     $('#all-categories').on('click','li', function(e){     
      localStorage.setItem('postData', JSON.stringify(data.posts[$(this).index()])); 
     }); 
    }); 


}, 
+0

請不要使用'$(el).attr('id')' - 只需使用'el.id'! – Alnitak

+0

嘗試將'alert(1)'插入'success'函數,'alert(0)'插入'error'函數。顯示哪個? – Curious

+0

in console shows this:Uncaught TypeError:Can not call method'then'of undefined,it means that do not find this:getCategory()。then(function(data){(##all-categories')。on ('click','li',function(e){localStorage.setItem('postData',JSON.stringify(data.posts [$(this).index()])); }); }) ; – pablo

回答

3

你可以試試這個:

$('#rock, #electronica, #pop').click(function (e) { 
    e.preventDefault();  //stop native link clicking 
    valueCat = this.id; 

    //then put your ajax call here, inside this function... 
}); 
0
$('a').click(function() { 
     var cat=$(this).attr('id'); 
     $.ajax({   
     url: 'http://.....org/api/get_category_posts', 
     data:'category_name='+cat+'&count=5&order=desc', 
     type: 'GET', 
     dataType: 'jsonp', 
     success: function(data){    
      var source = $("#category-template").html(); //Get the HTML from the template in the script tag 
      var template = Handlebars.compile(source); // compilamos la plantilla 
      var blogData = template(data); // en data se almacena el contenido que nos viene de wordpress 
      $('#category-data').html(blogData); // mostramos el contenido 
      $('#category-data').trigger('create'); 
      dfd.resolve(data); 

     }, 

     error: function(data){ 
      console.log(data); 
     } 
    }); 
}); 

你的AJAX功能對link.you的點擊被稱爲被外界稱之爲

+0

非常感謝你們兩個,我嘗試了兩種選擇,但沒有任何反應,也許是因爲我沒有發佈完整的功能,我在上面添加了 – pablo