2011-04-27 129 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:然后使用'

相关问题