我想将一个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);
}
我试图通过这两个i
和json[i].title
到formatDescrip()
但它抛出这个错误:
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
对象呢?我会怎么做呢?它似乎应该是直截了当的,但我不断收到错误。
错误很明显。看看这个:'formatDescrip(3,如何利用电子邮件营销)'。这是无效的JavaScript。它应该是'formatDescrip(3,'如何利用电子邮件营销')'所以你缺少引号。 – 2011-04-27 14:20:23
请不要在2011年使用'javascript:void(0)'onclick'属性...我的眼睛在流血。 – Capsule 2011-04-27 14:22:05
胶囊 - 你能告诉我我应该做什么吗? – 2011-04-27 14:23:57