【发布时间】:2012-05-20 19:51:03
【问题描述】:
我正在尝试在选中复选框时更改按钮的类,但我无法正确设置它。当复选框被选中时,我想将按钮类从 buttonshide 更改为 buttonshow。
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>
仅供参考...这两个 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");
}
});
});
【问题讨论】:
-
你已经清楚地解释了你想要什么,这太棒了。但是你得到了什么错误?你看到的行为是什么?告诉我们这是在做什么。给我们一个开始的地方。
-
您要添加的
selected应该是什么类? -
.selected 用于复选框文本颜色,正如您在小提琴中看到的那样正常工作,但我需要深入到按钮并更改其类
标签: jquery