JavaScript Object / Array новичок (используя jQuery) - PullRequest
0 голосов
/ 03 февраля 2011

У меня есть функция, которая должна возвращать массив или объект.Я не уверен, что лучше, и я не уверен, как его построить.Вот что у меня так далеко.Обратите особое внимание на комментарии во ВСЕХ КОПИЯХ.Идея состоит в том, чтобы пройти через набор ячеек td, которые содержат атрибут «data-field», и использовать имя атрибута в качестве имени переменной, а текст, содержащийся в 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
}

Ответы [ 2 ]

2 голосов
/ 03 февраля 2011

Я полагаю, что что-то подобное будет работать для ваших целей.

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;
}
1 голос
/ 03 февраля 2011

Попробуйте это:

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...