4

私は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" /> 
4

2 に答える 2

0

わかりました、これはカルマの問題であり、jquery の問題ではないと言わざるを得ません。

Backbone.js を使用しており、ビューはビューの定義にテンプレートをロードしています。

template : _.template($("#mytemplate").html()),

フィクスチャが見つからない場合、カルマはテストをロードするときに例外になるため、起動時にフィクスチャを「fixtures.js」というファイルに追加しました。

loadFixtures('viewtemplate.html','mytemplate.html');

(@Blame)誰かbeforeEachがジャスミン テスト スイートに次のコードを追加しました:

beforeEach( function() {
    loadFixtures('currentStepContainerFixture.html', 'mytemplate.html');

そのため、フィクスチャは2 回ロードされました。

コードで使用していたとき:

 console.log($(this).attr('id') + " " + $(this).val()); 
 console.log($(this).attr('id') + " " + $("#"+$(this).attr('id')).val());

基本的に、最初のログは現在の要素に関するものでしたが、2 番目のログはその ID を持つ最初の入力に関するものでした。

それが、私たちがこの奇妙な振る舞いをした理由です。同じ id を持つ 2 つの要素が存在する場合に何らかの例外がスローされると、役に立ちました。

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"

問題はモニターとキーボードの間にありました。ごめん :(

于 2013-08-02T12:44:01.950 に答える