JavaScript 对象/数组新手(使用 jQuery)
我有一个需要返回数组或对象的函数。我不确定哪个是最好的,也不知道如何构建它。这是我到目前为止所拥有的。请密切注意全部大写的评论。这个想法是循环遍历一组包含“数据字段”属性的 td 单元格,并使用该属性的名称作为变量名称,并将 td 中包含的文本作为值。数组/对象的属性或值的数量未知。根据单击进入该功能的内容,需要 2-6 个值。
function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();
// DEFINE SOME ARRAY OR OBJECT HERE
// put original values in the newEntry Array with a loop, using the
// data-field attributes as the name of the array or object variables
// and the text of the td as the value
$('#'+rowID+' td[data-field]').each(function() {
var field = $(this).attr('data-field');
var value = $(this).text();
// BUILD OUT OBJECT OR ARRAY
});
// RETURN ARRAY OR OBJECT
}
I have a function which needs to return either an array or object. I'm not sure which is best and I'm not sure how to construct it. Here's what I have so far. Pay close attention to the comments in ALL CAPS. The idea is to loop through a set of td cells that contain a 'data-field' attribute, and use the name of the attribute as the variable name and the text contained within the td as the value. The number of properties or values of the array/object are unknown. Depending on what was clicked to enter into the function, 2-6 values will be needed.
function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();
// DEFINE SOME ARRAY OR OBJECT HERE
// put original values in the newEntry Array with a loop, using the
// data-field attributes as the name of the array or object variables
// and the text of the td as the value
$('#'+rowID+' td[data-field]').each(function() {
var field = $(this).attr('data-field');
var value = $(this).text();
// BUILD OUT OBJECT OR ARRAY
});
// RETURN ARRAY OR OBJECT
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这样的东西可以满足您的目的。
I believe something like this would work for your purposes.
试试这个:
Try this: