如何使用 jQuery .each() 创建 JSON 数组或对象
我看过很多关于如何使用 jQuery 和 .each() 解析 json 数组的示例。我想做的是使用 .each() 函数创建 json。我正在从用户那里获取有关文本框的输入,这些文本框将在 ajax 调用中用于对表进行排序。我使用 .each() 查找所有具有有效输入的文本框,然后我想将该数据放入 json 数据类型并通过 ajax 将其发送到 SQL 查询。我不确定我是否想要一个对象还是一个数组,并且从迄今为止我解析的数据来看,我的对象似乎并不包含我期望的所有内容。
我创建了一个全局对象来存储数据,但是网上的一篇文章显示了括号,当时我看到的数组的所有内容都是括号 ( ),而对象是大括号 { }。所以我的全局“对象”(因为我不知道该调用什么[])此时看起来像这样:
var jsonFilter = [];
对于 .each() 函数,它查看用户数据“onkeyup”来过滤列表:
$("input:text[id^='filter']").live({
focusin: function () {
currentFilter = $(this).val();
$(this).removeClass("filter");
$(this).val("");
},
focusout: function () {
if ($(this).val() == "") {
// If they gave up focus and didn't type anything in the box, restore the text
$(this).addClass("filter");
$(this).val(currentFilter);
}
},
keyup: function () {
$("input:text[id^='filter']").each(function () {
if ($(this).val() != "" && !$(this).hasClass('filter')) {
//alert("filterBy: " + $(this).attr('id') + "\nfilterValue: " + $(this).val());
jsonFilter.push({ filterBy: $(this).attr('id'), filterValue: $(this).val() });
}
});
//alert("Done with .each()");
filterReport(jsonFilter);
}
});
对于输出 jsonFilter 内容的数组:
function filterReport(_jsonFilter) {
$("#jsonFilterLength").html(_jsonFilter.length);
//$("#filterTest").html("");
for (var i = 0; i < _jsonFilter.length; i++) {
$("#filterTest").append("Index: " + i + ", filterBy: " + _jsonFilter[i]["filterBy"] + ", filterValue: " + _jsonFilter[i]["filterValue"] + "<br /><br />");
}
}
当我查看 div“#jsonFilterLength”中的 html 时,发现长度在增加,因此它似乎正在存储附加数据。但是 div“#filterTest”中的 html 仅显示我正在输入的当前文本框的数据,而不是我期望整个 json 数组包含的数据,因为我试图允许同时过滤多个文本输入。
问候, 斯科特
I've seen a lot of examples that site how to use jQuery and .each() to parse json arrays. What I want to do is use the .each() function to create the json. I'm getting input from the user regarding text boxes that will be used in an ajax call to sort a table. I am using .each() to find all text boxes that have valid input, and then I want to put that data into a json data type and send it via ajax to an SQL query. I'm not certain if I want an object vs. an array, and from what data I've parsed thus far, my object doesn't seem to contain everything I'd expect.
I've created a global object to store the data, but a previous post on the web shows brackets, when everything I've seen for an array is parens ( ), and an object is curly braces { }. So my global 'object' (since I don't know what to call [ ]) at this point looks like this:
var jsonFilter = [];
For the .each() function, that looks at user data 'onkeyup' to filter the list:
$("input:text[id^='filter']").live({
focusin: function () {
currentFilter = $(this).val();
$(this).removeClass("filter");
$(this).val("");
},
focusout: function () {
if ($(this).val() == "") {
// If they gave up focus and didn't type anything in the box, restore the text
$(this).addClass("filter");
$(this).val(currentFilter);
}
},
keyup: function () {
$("input:text[id^='filter']").each(function () {
if ($(this).val() != "" && !$(this).hasClass('filter')) {
//alert("filterBy: " + $(this).attr('id') + "\nfilterValue: " + $(this).val());
jsonFilter.push({ filterBy: $(this).attr('id'), filterValue: $(this).val() });
}
});
//alert("Done with .each()");
filterReport(jsonFilter);
}
});
And for the array that is outputting the content of the jsonFilter:
function filterReport(_jsonFilter) {
$("#jsonFilterLength").html(_jsonFilter.length);
//$("#filterTest").html("");
for (var i = 0; i < _jsonFilter.length; i++) {
$("#filterTest").append("Index: " + i + ", filterBy: " + _jsonFilter[i]["filterBy"] + ", filterValue: " + _jsonFilter[i]["filterValue"] + "<br /><br />");
}
}
When I view the html in the div "#jsonFilterLength", is see that the length is increasing, so it appears to be storing additional data. But the html in the div "#filterTest" only shows the data for the current text box I'm typing in, not what I would expect the entire json array to contain as I am trying to allow filtering on multiple text inputs simultaneously.
Regards,
Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎为我工作。
http://jsfiddle.net/WQjsN/2/
确保清除 keyup 函数上的 jsonFilter 变量。
您还可以修改代码以使其更短。
例如。
Seems to be working for me.
http://jsfiddle.net/WQjsN/2/
Make sure you clear the jsonFilter variable on keyup function.
You can also modify the code so that it's shorter.
Eg.
我不太明白你在做什么,但要构建/解析 json,我建议你使用 Douglas Crockford 的 json2 库。
所以基本上你会构建一个 Javascript 对象来存储你想要发布的数据,然后调用:
var jsonToPostToTheServer = JSON.stringify(myDataObjectToPostToTheServer);
PS,任何时候你说“我添加了一个全局对象”,你都做错了。
I don't really get what you're doing exactly but to build / parse json I'd recomend you use Douglas Crockford's json2 library.
So basically you'd build up a Javascript object to store the data you want to post up then call:
var jsonToPostToTheServer = JSON.stringify(myDataObjectToPostToTheServer);
PS at any point you say "I've added a global object" you're doing it wrong.
首先,如果我是你,我会使用浏览器内调试器来查看数组/对象的内容。尝试 IE 中的内置功能(通过 F12 键)或 Firefox 中的 firebug,您可以轻松设置 JavaScript 代码的断点并查看发生了什么。
至于你的 json 内容,你应该能够在你想要调用 Ajax 函数时一次性 jsonify 你的数组/对象。所以你现在只需要集中精力构建数组/对象
Firstly, if I were you I'd use an in- browser debugger to look at the content of your array/object. Try the built in stuff in IE (via the F12 key) or firebug in firefox, you can easily set breakpoints of JavaScript code and look at what is happening.
As for your json stuff, you should be able to just jsonify your array/object in one go at the point you want to call the Ajax function. So you just need to concentrate of building up the array/object for now