チェックボックスがチェックされているときにチェックボックスの横に新しいdivタグを追加する方法と、2つのチェックボックスがチェックされているときに、2つのdivタグを表示する必要があります。jqueryを使用してこのモジュールを解決するのを手伝ってください
4395 次
1 に答える
3
$(':checkbox').click(function () {
if ($(this).attr('checked')) {
// create new div
var newDiv = $('<div>contents</div>');
// you can insert element like this:
newDiv.insertAfter($(this));
// or like that (choose syntax that you prefer):
$(this).after(newDiv);
} else {
// this will remove div next to current element if it's present
$(this).next().filter('div').remove();
}
});
この新しい div をチェックボックスのラベルの横に追加したくない場合は、最初にチェックボックスに ID が設定されていることと、ラベルの for 属性を使用してラベルをチェックボックスに接続していることを確認してください。
<label for="myCb1">test</label>
<input type="checkbox" id="myCb1" value="1" />
上記の JS コードを少し変更するだけで完了です。
$(':checkbox').click(function () {
// current checkbox id
var id = $(this).attr('id');
// checkbox' label
var label = $('label[for=' + id + ']');
if ($(this).attr('checked')) {
// create new div
var newDiv = $('<div>contents</div>');
// insert div element
newDiv.insertAfter(label);
} else {
// this will remove div next to current element if it's present
label.next().filter('div').remove();
}
});
于 2009-07-30T05:34:35.227 に答える