2011-04-27 148 views
1

我想將一個JSON元素傳遞給一個函數,但它不工作。有什麼想法嗎?這裏是我的代碼:在jQuery中傳遞一個對象作爲函數參數

$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "/smallbusiness/_assets/js/events.json", 
     success: formatJson, 
     error: function() { 
      alert("Data file failed to load"); 
     } 
    }); 
}); 

function formatJson (data) { 

     var json = data.events.event; 
     var containerList = $('#event-list'); 
     var containerDescrip = $('#event-descrip'); 
     var template = ''; 
     var defaultDescrip = ''; 

     //Format and display event list 
     $.each(json, function(i, item) { 
      template += '<p><a href="javascript:void(0)" onClick="formatDescrip(' + i + ',' + json[i].title + ')">' + this.title + '</a></p>'; 
     }); 
     containerList.html(template); 
} 

function formatDescrip(j, jsonData) { 
    alert(j); 
    alert(jsonData); 
} 

我試圖通過這兩個ijson[i].titleformatDescrip()但它拋出這個錯誤:

Error: missing) after argument list 
Source File: http://localhost/smallbusiness/webinars.html# 
Line: 1, Column: 20 
Source Code: 
formatDescrip(3,How to Leverage Email Marketing) 

我在做什麼錯?如果我想通過整個json對象呢?我會怎麼做呢?它似乎應該是直截了當的,但我不斷收到錯誤。

+1

錯誤很明顯。看看這個:'formatDescrip(3,如何利用電子郵件營銷)'。這是無效的JavaScript。它應該是'formatDescrip(3,'如何利用電子郵件營銷')'所以你缺少引號。 – 2011-04-27 14:20:23

+1

請不要在2011年使用'javascript:void(0)'onclick'屬性...我的眼睛在流血。 – Capsule 2011-04-27 14:22:05

+0

膠囊 - 你能告訴我我應該做什麼嗎? – 2011-04-27 14:23:57

回答

2

你忘了標題周圍的引號。然而,爲什麼不使用jQuery的「.delegate()」來設置處理程序呢?爲什麼不使用jQuery的「.delegate()」呢?

$.each(json, function(i, item) { 
     template += '<p><a class="dynamic" data-index="' + i + '" href="#">' + this.title + '</a></p>'; 
    }); 
    containerList.delegate("a.dynamic", "click", function(ev) { 
     formatDescrip($(this).data('index'), $(this).text()); 
    }); 

或類似的東西;如果列表被多次擴展,那麼「.delegate()」調用可能應該在處理程序之外進行一次。

編輯 —如果「formatDescrip()」函數需要訪問原來的「事件」對象(無論這些事情是你用它來使<a>標記列表),你可以通過它來「formatDescrip ()」,而不是指數,然後再修改其他功能需要:

containerList.delegate("a.dynamic", "click", function(ev) { 
     formatDescrip(json[$(this).data('index')]); 
    }); 

    // ... 

function formatDescrip(eventObj) { 
    alert(eventObj.title); 
    // 
    // ... more code here ... 
    // 
} 
+0

您的解決方案效果很好,謝謝!我今天學到了關於jQuery的新東西。 :) – 2011-04-27 15:40:41

+0

還有一個問題......如果不是隻傳遞標題,而是想傳遞整個對象? – 2011-04-27 15:43:27

0

我想,也許你有一個簡單的報價爲標題的一部分,是打破你的代碼。嘗試在字符串中使用這些字符,或者使用雙引號代替簡單的字符。

1

我已經在我的評論中解釋了什麼是問題(缺少引號)。

但是最好不要這樣創建HTML,而是創建「真實」元素。它更容易使用jQuery:

var $div = $('<div />'); 
//Format and display event list 
$.each(json, function(i, item) { 
    var title = json[i].title; 
    $('<p />').append(
     $("<a />", { 
      href: "javascript:void(0)", // you should use a proper URL here 
      text: title, 
      click: function(e){ 
       e.preventDefault(); 
       formatDescrip(i, title); 
      } 
     }) 
    ).appendTo($div); 
}); 
containerList.empty().append($div.children()); 

更新:或者甚至更好,使用.delegate()@Pointy suggests

+0

如果沒有合適的URL有意義,該怎麼辦? – 2011-04-27 14:42:21

+0

@ellenchristine:然後使用'

相關問題