Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:如何为每个项创建具有两个值的数组_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript jQuery:如何为每个项创建具有两个值的数组

Javascript jQuery:如何为每个项创建具有两个值的数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我是jQuery新手,希望有人能帮我解决这个问题,并提供一个简短的解释,以便我将来可以将其应用于类似的情况 我有一个动态构建的大型HTML页面。 该页面包含多个具有某些可编辑div的表(contenteditable=true)。这些div都有类“editable” 现在,我想为所有这些div创建一个数组,其中包含它们的id和内容(文本) 到目前为止,我有以下内容,它们应该为这些div创建一个递增的唯一id,但我不确定如何为此创建数组。 另外,出于好奇,是否有一个特定的术语,如何使用每个项两个值

我是jQuery新手,希望有人能帮我解决这个问题,并提供一个简短的解释,以便我将来可以将其应用于类似的情况

我有一个动态构建的大型HTML页面。 该页面包含多个具有某些可编辑div的表(contenteditable=true)。这些div都有类
“editable”

现在,我想为所有这些div创建一个数组,其中包含它们的id和内容(文本)

到目前为止,我有以下内容,它们应该为这些div创建一个递增的唯一id,但我不确定如何为此创建数组。 另外,出于好奇,是否有一个特定的术语,如何使用每个项两个值来调用这样的数组

我的jQuery:

$('#btnSave').on('click', function(){
    var i = 0;
    $(this).closest('form').find('div.editable').each(function(){
        $(this).attr('id', 'ed' + i+1);
        if( ($(this).text != '') && ($(this).text != ' ') ){
            $(this).addClass('edited');
        }
        i++;
    });
});

// my attempt for the array (perhaps the wrong approach):
var arrEdited = new Array();
$('div.edited').each(function(){
    arrEdited.push($.trim($(this).text()));
});
多谢各位,
Mike

您应该使用对象数组
在数组中存储div
id
text

选中此项:

// my attempt for the array (perhaps the wrong approach):
var arrEdited = []; // [] is better than new Array()

$('div.edited').each(function () {
    // Add this div id and content in array
    arrEdited.push({
        id: $(this).attr('id'),
        text: $.trim($(this).text())
    });
});
可以使用创建数组

通过函数传递当前匹配集中的每个元素,生成包含返回值的新jQuery对象

由于返回值是一个jQuery对象,其中包含一个数组,因此通常对结果调用
.get()
,以使用基本数组


我认为您不需要另一个循环,相反,您可以将它放在第一个循环中,
if($(this.text()!='')和($(this.text()!='')中
,然后将一个
对象
推送到数组中,而不是一个值

var arrEdited = new Array();
$('#btnSave').on('click', function(){
    $(this).closest('form').find('div.editable').each(function(index){
        //you could use the index when you use .each function
        $(this).attr('id', 'ed' + (index+1));
        if( ($(this).text() != '') && ($(this).text() != ' ') ){
            $(this).addClass('edited');
            //instead of using another loop, you can put your code here
            arrEdited.push({
                id: $(this).attr('id'),
                text: $.trim($(this).text())
            });
            //here you use an object, people call it array of objects
        }
    });
});

不是
$(this).text
,它应该是
$(this).text()
,也谢谢你。你能解释一下为什么[]比新阵列更好吗?谢谢!你说得对,这对我来说更好——也谢谢你的解释。我将尽快接受此信息。:)只需补充一点:“attr”前面缺少一个点-我在我的帖子中更正了这一点。@keewee279,最好使用
索引
而不是
I
,@Satpal:谢谢!可以。:)@keewee279,删除
i++同时使用代码并将
$(this)
缓存在变量中,如
var\u this=$(this)并在每个块中使用此
var arrEdited = new Array();
$('#btnSave').on('click', function(){
    $(this).closest('form').find('div.editable').each(function(index){
        //you could use the index when you use .each function
        $(this).attr('id', 'ed' + (index+1));
        if( ($(this).text() != '') && ($(this).text() != ' ') ){
            $(this).addClass('edited');
            //instead of using another loop, you can put your code here
            arrEdited.push({
                id: $(this).attr('id'),
                text: $.trim($(this).text())
            });
            //here you use an object, people call it array of objects
        }
    });
});