div内の画像を追加および削除するために使用しているラジオボタンのセットがあります。私の画像ソースは、ラジオボタン内のデータセット値にあります。
<input class="radioGroup" name="radioGroup" type="radio" id="radio1" data-source-image="/image1.jpg" data-source-desc="This is the First Image">
<label for="#radio1">Image 1</label><br />
<input class="radioGroup" name="radioGroup" type="radio" id="radio2" data-source-image="/image2.jpg" data-source-desc="This is the Second">
<label for="#radio2">Image 2</label>
ラジオボタンIDに対応するクラスを画像に追加し、チェックされていない場合はそれを使用して画像を削除します。
$('.selections input').live("change", function () {
// 'Getting' data-attributes using dataset
var appendImg = $(this).data('sourceImage');
var itemDesc = $(this).data('sourceDesc');
var item = $(this).attr('id');
if ($(this).is(':radio')) {
var radioGroupName = $(this).attr('name');
var radioGroup = $('input[name="' + radioGroupName + '"]')
radioGroup.each( function() {
if ($(this).attr("checked")) {
$('.imageContainer').append('<img src="' + appendImg + '" alt="' + itemDesc + '" class="' + item + '" />');
}
if ( ! $(this).attr("checked")){
$('.imageContainer').children('img').remove('.' + item);
}
});
} });
これを機能させることはできませんが、このコードの複数のバリエーションを試しましたが、それぞれわずかに異なる結果が得られましたが、期待どおりに機能するものはありませんでした。このコードの場合、最初のラジオボタンはまったく機能せず、2番目のラジオボタンはその画像を追加するだけです。
また、それをクリーンアップするための他の提案も歓迎されます(この関数で処理している他の入力があるため、私のラジオチェックがあります)。
ありがとう!