Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.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中每个循环中$(this)和$(id)之间的差异_Javascript_Jquery_Jquery Selectors - Fatal编程技术网

Javascript jquery中每个循环中$(this)和$(id)之间的差异

Javascript jquery中每个循环中$(this)和$(id)之间的差异,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我不熟悉jquery。我知道这个问题很愚蠢,但我在这里遗漏了一些概念。 问题是: $("input.integers").each(function(index) { console.log("----"); console.log($(this).attr('id') + " " + $(this).val()); console.log($(this).attr('id') + " " + $("#"+$(this).attr('id')).val());

我不熟悉jquery。我知道这个问题很愚蠢,但我在这里遗漏了一些概念。 问题是:

$("input.integers").each(function(index) {

    console.log("----");
    console.log($(this).attr('id') + " " + $(this).val()); 
    console.log($(this).attr('id') + " " + $("#"+$(this).attr('id')).val());
    // the next log is here just to show a direct selection. I'm concerned about the two logs above this one 
    console.log($("#myId").attr('id'), $("#myId").val());

    that.setModelData($(this).attr('id'), $(this).val());
});
以下是输出:

PhantomJS 1.6 (Linux) LOG: '----'
PhantomJS 1.6 (Linux): Executed 61 of 90
PhantomJS 1.6 (Linux) LOG: 'myId '
PhantomJS 1.6 (Linux): Executed 61 of 90
PhantomJS 1.6 (Linux) LOG: 'myId 123'
PhantomJS 1.6 (Linux): Executed 61 of 90
PhantomJS 1.6 (Linux) LOG: [ 'myId', '123' ]
标签被输入。为什么
$(this).val()
是空的而
$(“#”+$(this).attr('id')).val()包含正确的值

更新:

业力测试:

it('the model must be updated', function(){

    $("#myId").val("123");  

    $("#save-process").click();
    server.respond();

    expect(fdtView.model.get('myId')).toBe("123");

});
夹具:

<input id="myId" name="myId"
    class="integers" type="text" /> 

在循环中,与$(“input.integers”)选择器匹配。因此,jquery在内存中有这个选择器$(“#myId”)是一个唯一的选择器,对于每个循环,jquery都会搜索并分析该元素。这两个元素相同只是巧合,因为您的html代码是:

<input class="integers" id="myId" />
控制台日志:

Value = toto
Value = titi
Value = tata
Value = tutu

所以为了回答您的问题,如果
this.val()
$(“#”+$(this.attr('id')).val())
不同,那么加载它的选择器不好。

好的,我必须说这是karma的问题,而不是jquery的问题

我们正在使用Backbone.js,视图正在视图定义中加载模板:

template : _.template($("#mytemplate").html()),
Karma加载时,如果找不到夹具,测试将异常,因此我们在名为fixtures.js的文件中添加了启动时的夹具:

loadFixtures('viewtemplate.html','mytemplate.html');
(@bull)有人在jasmine测试套件的每个
之前添加了以下代码:

beforeEach( function() {
    loadFixtures('currentStepContainerFixture.html', 'mytemplate.html');
因此,装置装载了两次

当我在代码中使用时:

 console.log($(this).attr('id') + " " + $(this).val()); 
 console.log($(this).attr('id') + " " + $("#"+$(this).attr('id')).val());
基本上,第一个日志是关于当前元素的,而第二个日志是关于具有该id的第一个输入的

这就是为什么我们会有这种行为。我必须说,如果存在两个具有相同id的元素,则抛出某种异常可能会有所帮助


我不配得到这四分,问题出在显示器和键盘之间。抱歉:(

你确定第一个日志的值是空的吗..因为我觉得这很好。@BalintBako ID是唯一的它像一个符咒一样工作:@bipen不,第一个日志是'myId',所以不打印任何内容,第二个日志是'myId 123'@BalintBako Yep,也适用于我(Chrome 28)。我应该使用$(这个)[索引]?我的意思是,我一直认为这是指由$(“input.integers”)选择的集合的当前元素。为什么要使用索引。通过选择器,jquery使用它来定义唯一的元素。我添加了一个具有多个输入的示例。您说的是
this
匹配
$(“input.integers”)
选择器但这不是真的:在
每个
回调中,
这个
是当前输入,而不是整个集合!我想我应该澄清第三个打印只是为了显示元素的直接选择。我关心的是第一个记录消息-@MatteoTassinari确切地说,这就是为什么我有e后续问题。
beforeEach( function() {
    loadFixtures('currentStepContainerFixture.html', 'mytemplate.html');
 console.log($(this).attr('id') + " " + $(this).val()); 
 console.log($(this).attr('id') + " " + $("#"+$(this).attr('id')).val());
PhantomJS 1.6 (Linux): Executed 61 of 90
PhantomJS 1.6 (Linux) LOG: 'myId ' --> current $(this)
PhantomJS 1.6 (Linux): Executed 61 of 90
PhantomJS 1.6 (Linux) LOG: 'myId 123' --> first occurrence of "#myId"