1

jQueryを使用して、配列を反復処理して値と比較しようとしていますが、困惑している問題に直面しています。コードは次のとおりです。

var cF = ['first','last','email'];
var fFound = 0;
cj(this).children('.section').each(function(f){
  var fId = cj(this).attr('class');
  fId = fId.replace('-section', '');
  console.log('fId: ',fId);
  cj.each(cF, function( cFi, cFv ){
    console.log('cFv: ',cFv);
    if ( cFv == fId ) {
      console.log('found!');
      fFound = 1;
    }
  });
  console.log('fFound: ',fFound);
});

基本的に私がやっていることは、一連の div ('this' で渡される) を検索し、クラス名を取得し、クラスのサフィックスを取り除き、それを私の cF 配列と比較することです。見つかった場合は、fFound を 1 に設定します (値が存在することを示すフラグです)。

もともと私は jQuery.inArray() を使用していましたが、あまり運がありませんでした。fId と cFv をコンソールに送信すると、それらは一致しますが、比較では一致していると認識されません。

4

3 に答える 3

1

これを試して

cj.each(cF, function( cFi, cFv ){
    console.log('cFv: ',cFv);
    if ( $.inArray( fId, cF )  > -1 ) {
      console.log('found!');
      fFound = 1;
    }
  });
于 2012-09-24T18:25:31.557 に答える
0
var cF = ['first','last','email'];
var fFound = 0;
cj(this).children('.section').each(function(f){       
  var fId = cj(this).attr('class');
  fFound = 0; // reset 
  fId = fId.replace('-section', '').trim();
  console.log('fId: ',fId);
  if( $.inArray( fId, cF ) !== -1 ) {
      console.log('found!');          
   }
   console.log('fFound: ',fFound);
});
于 2012-09-24T18:23:42.630 に答える
0

配列を反復処理して値を比較する

var cF = ['first','last','email'];
var bla = 'anything'
var fFound = 0;

for (j in cF) {
    if (bla.indexOf(cF[j]) > -1) {
        fFound = 1;
    }
}

ops...あなたはjQueryが欲しい...それをスクラッチします。

于 2012-09-24T18:23:08.830 に答える