チェックボックスがオンになっているときにボタンのクラスを変更しようとしていますが、正しく設定できません。チェックボックスが選択されているときに、buttonsクラスをbuttonshideからbuttonsshowに変更します。
HTML
<div class="checkbox">
<input id="ChartstoDisplayAll" type="checkbox" name="ctl00$MainContent$ChartstoDisplayAll" /><label for="ChartstoDisplayAll">Select All</label>
<p class="checkbox" style="margin-left:5px;"> or select individually below </p>
<hr class="horizline" style="width:870px;"/>
</div>
<div id="buttons" class="buttonshide">
<input type="image" name="ctl00$MainContent$btnGetChart" id="MainContent_btnGetChart" title="Click here to retrieve the charts" Text="Build the Charts" src="Styles/Images/stats.png" alt="Build the Charts" align="left" />
<p style="font-size:.8em; font-weight:bold;">Build Charts</p>
</div>
参考までに...これら2つのdivの間に他のhtmlがあります。ボタンのIDを持っているものはないので、簡潔にするために取り出しました。
これがjavascriptです。(私が試した最新の方法)
//this script will change the style of the "Select All" checkbox and make visible the "Build Chart" button
$(document).ready(function () {
$('#ChartstoDisplayAll').click(
function () {
$("INPUT[type='checkbox']").attr('checked', $('#ChartstoDisplayAll').is(':checked'));
$(this).next().toggleClass("selected");
$(this).find("buttons").toggleclass("buttonsshow");
});
});
これがフィドルリンクです http://jsfiddle.net/dinotom/h25tM/2/
これは、テキストの色を変更し、チェックボックスが選択されたときにボタンを表示する、完成した作業スクリプトです。
//this script will change the style of the "Select All" checkbox and make visible the "Build Chart" button
$(document).ready(function () {
$('#ChartstoDisplayAll').change(
function () {
if ($('#ChartstoDisplayAll').is(':checked')) {
$(this).next().addClass("selected");
$('#buttons').addClass("buttonsshow").removeClass('buttonshide');
} else {
$('#buttons').addClass("buttonshide").removeClass('buttonsshow');
$(this).next().removeClass("selected");
}
});
});