Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 each()索引值_Javascript_Jquery - Fatal编程技术网

Javascript each()索引值

Javascript each()索引值,javascript,jquery,Javascript,Jquery,几个月来,我一直在自学JavaScript和jQuery,但我仍然对JavaScript对象和jQuery对象感到困惑 在下面的示例中,我将jQuery对象分配给变量$target。$target应该由两个对象组成的数组组成 我的问题是为什么我必须在.each函数中将value变量再次包装到jQuery对象中 $('select.to_append').change(function(){ var $target = $('select.to_append'); var $for

几个月来,我一直在自学JavaScript和jQuery,但我仍然对JavaScript对象和jQuery对象感到困惑

在下面的示例中,我将jQuery对象分配给变量$target。$target应该由两个对象组成的数组组成

我的问题是为什么我必须在.each函数中将value变量再次包装到jQuery对象中

$('select.to_append').change(function(){
    var $target = $('select.to_append');
    var $form = $('#anotherForm');

    $.each($target, function(key, value){
        $form.append('<input name="' + $(value).attr('name') + '" type="hidden" value="' + $(value).val() + '"></input>');
    });
});
我用于从不是提交表单一部分的选择中附加值的示例代码

因为$target是一个jQuery对象,但是当您迭代时,您将在迭代处理程序中得到一个dom元素引用,而不是jQuery对象。因此,如果要访问该对象上的jQuery方法,需要再次包装该对象

通过迭代jQuery对象的方式,您可以使用


谢谢,我将查找这两个函数之间的差异。
$('select.to_append').change(function () {
    var $target = $('select.to_append');
    var $form = $('#anotherForm');

    $target.each(function (index, el) {
        $form.append('<input name="' + $(el).attr('name') + '" type="hidden" value="' + $(el).val() + '"></input>');
    });
});