JavaScript 对象/数组新手(使用 jQuery)

发布于 2024-10-16 02:05:54 字数 732 浏览 4 评论 0原文

我有一个需要返回数组或对象的函数。我不确定哪个是最好的,也不知道如何构建它。这是我到目前为止所拥有的。请密切注意全部大写的评论。这个想法是循环遍历一组包含“数据字段”属性的 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 技术交流群。

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

发布评论

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

评论(2

一曲琵琶半遮面シ 2024-10-23 02:05:54

我相信这样的东西可以满足您的目的。

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

// DEFINE SOME ARRAY OR OBJECT HERE
var buildup = {};

// 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
    buildup[field] = value;
});
// RETURN ARRAY OR OBJECT

return buildup;
}

I believe something like this would work for your purposes.

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

// DEFINE SOME ARRAY OR OBJECT HERE
var buildup = {};

// 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
    buildup[field] = value;
});
// RETURN ARRAY OR OBJECT

return buildup;
}
泪是无色的血 2024-10-23 02:05:54

试试这个:

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

var toReturn = {};

// 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();

    toReturn[field] = value;
});
// RETURN ARRAY OR OBJECT
return toReturn;
}

Try this:

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

var toReturn = {};

// 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();

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