如何使用 jQuery Click 事件处理 $.getJSON 中的 JSON 对象?

发布于 2024-12-04 20:04:15 字数 1097 浏览 1 评论 0原文

我不确定这是否是一个愚蠢的问题...我正在尝试使用 jQuery 单击事件对 JSON 数组进行排序。以下是我认为应该有效的简化 html 代码:

<SCRIPT type='text/javascript'>

function results(json) {
// display unsorted results in $('#results')
}

$('#sort').click(function (json) {
// sort and display results in $('#results')
});

var query = some_query;
var json = new Object();
$.getJSON(query, results);

</SCRIPT>

<A href='javascript:void(0)' id='sort'>Sort</A>
<DIV id='results'></DIV>

单击“排序”链接后没有收到任何响应。当我用alert(json)替换排序函数时,出现json未定义错误。我在这里错过了什么吗?

进一步更新:

我已经修改了代码,这应该可以工作。感谢大家的贡献:)

<SCRIPT type='text/javascript'>

function results(json) {
// display unsorted results in $('#results')
$('#sort').click(function() {sort(json);});
}

function sort(A) {
var B = A.sort(sorting()); // function to sort the array
results(B);
}

var query = some_query;
$.getJSON(query, results); // triggered by listener

</SCRIPT>

<A href='javascript:void(0)' id='sort'>Sort</A>
<DIV id='results'></DIV>

I am not sure whether this is a silly question... I am trying to sort a JSON array using jQuery click event. The following is a simplified html code which I thought should work:

<SCRIPT type='text/javascript'>

function results(json) {
// display unsorted results in $('#results')
}

$('#sort').click(function (json) {
// sort and display results in $('#results')
});

var query = some_query;
var json = new Object();
$.getJSON(query, results);

</SCRIPT>

<A href='javascript:void(0)' id='sort'>Sort</A>
<DIV id='results'></DIV>

I am getting no response upon clicking the Sort link. When I replace the sort function with alert(json), I get a json is undefined error. Am I missing out something here?

Further update:

I have amended the code and this should work. Thanks everyone for your contribution :)

<SCRIPT type='text/javascript'>

function results(json) {
// display unsorted results in $('#results')
$('#sort').click(function() {sort(json);});
}

function sort(A) {
var B = A.sort(sorting()); // function to sort the array
results(B);
}

var query = some_query;
$.getJSON(query, results); // triggered by listener

</SCRIPT>

<A href='javascript:void(0)' id='sort'>Sort</A>
<DIV id='results'></DIV>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-12-11 20:04:15

您只需要进行一些更改:

<SCRIPT type='text/javascript'>

function results(data, textStatus, jqXHR) {
  // display unsorted results in $('#results')
  var json = data;
}

$('#sort').click(function () {
  var query = some_query;
  $.getJSON(url, query, results);
});

</SCRIPT>

<A href='javascript:void(0)' id='sort'>Sort</A>
<DIV id='results'></DIV>

当从您发送 $.getJson() 请求的 URL 收到成功的 JSON 响应时,将调用您的 results 函数。

You just need to make a couple of changes:

<SCRIPT type='text/javascript'>

function results(data, textStatus, jqXHR) {
  // display unsorted results in $('#results')
  var json = data;
}

$('#sort').click(function () {
  var query = some_query;
  $.getJSON(url, query, results);
});

</SCRIPT>

<A href='javascript:void(0)' id='sort'>Sort</A>
<DIV id='results'></DIV>

Your results function will be called when a successful JSON response is received from the url you send your $.getJson() request to.

君勿笑 2024-12-11 20:04:15

在您的单击处理程序中,您期望“json”作为参数。所以它正在参数范围内寻找它。您已将“json”定义为全局变量,但它不会在那里找到它,因为您正在寻找它作为参数。

因此,您要么需要将“json”传递给处理程序,要么需要删除参数期望。

In you click handler you are expecting "json" as an argument. So it is looking for it in the argument scope. You have defined "json" as a global variable, but it will not find it there because you are looking for it as an argument.

So you either need to pass "json" to the handler or you need to remove the argument expectation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文