jquery下钻

时间:2012-05-20 19:51:03

标签: jquery

我正在尝试在选中复选框时更改按钮类,但我无法正确操作。选中该复选框后,我想将按钮类更改为buttonshhow from buttonshide。

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");
              }
          });
  });

2 个答案:

答案 0 :(得分:1)

试试这个:

if ($('#ChartstoDisplayAll').is(':checked')) {
  $(this).find("buttons").addClass("buttonsshow").removeClass('buttonshide');
} else {
  $(this).find("buttons").addClass("buttonshide").removeClass('buttonsshow');
}

此外,您应该使用change事件而不是click

答案 1 :(得分:0)

基本上这应该有所帮助:

jsFiddle demo

$('#ChartstoDisplayAll').on('change', function() {
    $(this).next('label').toggleClass("selected");      
    $("input[type='image']").toggleClass("buttonsshow");      
});

BTW:我改变了你的CSS并添加了一些评论,看看演示

相关问题