2012-02-26 11 views
0

のコンテンツにアクセス:難し私はこのようなjQueryのでは、配列にコントロールを追加していjQueryのアレイ

$('#tblControls tr').each(function() { 
var radioButtonText = $(this).find("#td2").find("#chxBxValue").val(); 
var isSelected = $(this).find("#td2").find("#chxBxSelected"); 
var $newRdBtn = ''; 

if (isSelected == 1) { 
    $newRdBtn = $('<input />').attr({ id: 'rdBtn', type: 'radio', name: 'rdBtngrp', selected: 'true' }) +'<label for="rdBtn">' + radioButtonText + '</label>'; 
} else { 
    $newRdBtn = $('<input />').attr({ id: 'rdBtn', type: 'radio', name: 'rdBtngrp' }) + '<label for="rdBtn">' + radioButtonText + '</label>'; 
} 

rblArray.push($newRdBtn); 
}); 

後で私はそうのような

$.each(rblArray, function (intIndex, objValue) { 
$("#myPlaceHolder").append(rblArray[intIndex]); 
$("#myPlaceHolder").append('<br/>'); 
}); 
にrblArrayの内容を追加しようとする別の関数に

しかし、これは

[object Object]Test Control 1 
[object Object]Test Control 2 

の代わり

を返します。 10
<input type="radio" name="rdBtngrp" id="rdBtn" />Test Control 1 
<input type="radio" name="rdBtngrp" id="rdBtn" />Test Control 2 

??私は何が欠けているのですか?

答えて

0

$('<input />').attr({ id: 'rdBtn', type: 'radio', name: 'rdBtngrp' })はそれを変更するオブジェクトです:

if (isSelected == 1) { 
    $newRdBtn = '<input id="rdBtn" type="radio" name="rdBtngrp" selected="true" />' +'<label for="rdBtn">' + radioButtonText + '</label>'; 
} else { 
    $newRdBtn = '<input id="rdBtn" type="radio" name="rdBtngrp" />' + '<label for="rdBtn">' + radioButtonText + '</label>'; 
} 
+0

アダムありがとうございます。それはうまくいった。 – Jason

0

一つの大きな問題は、IDのページ内で一意である必要があります。あなたのIDをクラスに変更してください。

"Test Control n"というテキストがどこから来たのかわからないようです。私はあなたの配列に既に存在する既存のjQueryオブジェクトとテキストを連結しようとしていると思います。テキストとオブジェクトを連結することはできません。 "$"でラップするのではなく、文字列として配列にプッシュするだけの場合は、

0

jQueryオブジェクトに文字列(+演算子)を追加するとエラーが発生します。

また、新しい要素を配列ではなくjQueryオブジェクトに蓄積することもできます(楽しいことではありません)。

このお試しください:それらを追加し、その後

$rbl = $();//empty jQuery object. 
$('#tblControls tr').each(function() { 
    var $td2 = $(this).find("#td2");//ids must be unique. Try class="td2" and ....find(".td2") instead 
    var radioButtonText = $td2.find("#chxBxValue").val(); 
    var isSelected = $td2.find("#chxBxSelected");//Not sure this is correct? 

    var inputProps = { id:'rdBtn', type:'radio', name:'rdBtngrp' };//ids must be unique for label's for="..." to work 
    if (isSelected == 1) { 
     inputProps.selected = true; 
    } 

    $span = $('<span></span>'); 
    $input = $('<input />').attr(inputProps); 
    $label = $('<label for="rdBtn">' + radioButtonText + '</label>');//id must match that established above 
    $rbl.add($span.append($input).append($label)); 
}); 

を:

$rbl.each(function() { 
    $("#myPlaceHolder").append(this).append('<br/>'); 
}); 

あなたが見ることができるように、あまりにも修正する他の物事のカップルがあります。

関連する問題