输入已检查更改按钮类

时间:2015-07-02 14:52:15

标签: javascript css checkbox input radio-button

想要从按钮中删除禁用类,并在选中单选按钮时添加启用类,并选中一个或多个复选框。

<div>
    <input name="select-customer" type="radio">btn 1
    <input name="select-customer" type="radio">btn 2
</div>
<div>    
    <input type="checkbox" name="choice-selection">Choice 1
    <input type="checkbox" name="choice-selection">Choice 2
    <input type="checkbox" name="choice-selection">Choice 3
    <input type="checkbox" name="choice-selection">Choice 4
</div>

https://jsfiddle.net/dLyvo7d0/

3 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$(document).ready(function(){
    $('#button').click(function(){
        $('input:radio').each(function () {
            var $this = $(this);
                if (($this).prop('checked')) {
                     $this.closest('span').addClass('enable');
                     $this.closest('span').removeClass('disable');
                }else{
                     $this.closest('span').addClass('disable');
                     $this.closest('span').removeClass('enable');
                }
        });
        $('input:checkbox').each(function () {
            var $this = $(this);
                if (($this).prop('checked')) {
                     $this.closest('span').addClass('enable');
                     $this.closest('span').removeClass('disable');
                }else{
                     $this.closest('span').addClass('disable');
                     $this.closest('span').removeClass('enable');
                }
        });
    });

});
&#13;
.disable {
    background: gray;
}

.enable {
    background: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
        <span><input name="select-customer" type="radio"/>btn 1</span>
        <span><input name="select-customer" type="radio"/>btn 2</span>
    </div>
    <div>    
         <span><input type="checkbox" name="choice-selection"/>Choice 1</span>
         <span><input type="checkbox" name="choice-selection"/>Choice 2</span>
         <span><input type="checkbox" name="choice-selection"/>Choice 3</span>
         <span><input type="checkbox" name="choice-selection"/>Choice 4</span>
    </div>
            <button class="disable" id="button">Button</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是一个纯JavaScript函数,用于侦听点击事件,然后根据输入更改按钮的类别&#39;点击状态:

&#13;
&#13;
> dput(sep)
structure(list(School = structure(1:38, .Label = c("Queens\\25Q020", 
"Queens\\25Q021", "Queens\\25Q022", "Queens\\25Q023", "Queens\\25Q024", 
"Queens\\25Q025", "Queens\\25Q029", "Queens\\25Q032", "Queens\\25Q079", 
"Queens\\25Q107", "Queens\\25Q120", "Queens\\25Q129", "Queens\\25Q130", 
"Queens\\25Q154", "Queens\\25Q163", "Queens\\25Q164", "Queens\\25Q165", 
"Queens\\25Q168", "Queens\\25Q169", "Queens\\25Q184", "Queens\\25Q185", 
"Queens\\25Q189", "Queens\\25Q193", "Queens\\25Q194", "Queens\\25Q200", 
"Queens\\25Q201", "Queens\\25Q209", "Queens\\25Q214", "Queens\\25Q219", 
"Queens\\25Q237", "Queens\\25Q242", "Queens\\25Q244", "Queens\\25Q425", 
"Queens\\25Q460", "Queens\\25Q499", "Queens\\25Q515", "Queens\\25Q707", 
"Queens\\25Q792"), class = "factor"), Latitude = c(40.758365, 
40.774168, 40.761748, 40.765602, 40.751762, 40.764834, 40.777951, 
40.76501, 40.785959, 40.755764, 40.745593, 40.789929, 40.760072, 
40.726437, 40.741093, 40.720926, 40.729642, 40.724191, 40.782346, 
40.778164, 40.775122, 40.760344, 40.792326, 40.78322, 40.73106, 
40.736521, 40.783714, 40.770194, 40.735855, 40.74943, 40.769753, 
40.754465, 40.738775, 40.764868, 40.738332, 40.735017, 40.72535, 
40.721466), Longitude = c(-73.824407, -73.818543, -73.811379, 
-73.828293, -73.81778, -73.789712, -73.842932, -73.794319, -73.817349, 
-73.799256, -73.829283, -73.839501, -73.783908, -73.807592, -73.808757, 
-73.823358, -73.81781, -73.80937, -73.77844, -73.799841, -73.8185, 
-73.817909, -73.809516, -73.806977, -73.805449, -73.813001, -73.795027, 
-73.82762, -73.823583, -73.82141, -73.832001, -73.826204, -73.823892, 
-73.826819, -73.82028, -73.821339, -73.811325, -73.820401), Windows.SEP.11 = c(48L, 
154L, 11L, 62L, 20L, 72L, 9L, 37L, 8L, 22L, 9L, 47L, 44L, 99L, 
78L, 91L, 42L, 122L, 55L, 14L, 162L, 108L, 89L, 87L, 23L, 14L, 
75L, 74L, 141L, 73L, 43L, 14L, 534L, 189L, 128L, 10L, 79L, 38L
), Mac.SEP.11 = c(49L, 0L, 180L, 2L, 202L, 116L, 41L, 1L, 17L, 
22L, 33L, 43L, 1L, 28L, 2L, 0L, 238L, 13L, 76L, 55L, 76L, 42L, 
0L, 1L, 12L, 0L, 16L, 10L, 1L, 7L, 0L, 1L, 1L, 67L, 16L, 7L, 
31L, 24L), Windows.SEP.12 = c(52L, 252L, 1L, 2L, 12L, 45L, 108L, 
15L, 14L, 4L, 19L, 21L, 46L, 90L, 10L, 86L, 15L, 76L, 122L, 2L, 
9L, 52L, 39L, 120L, 43L, 17L, 9L, 54L, 19L, 199L, 40L, 25L, 64L, 
164L, 14L, 27L, 45L, 2L), Mac.SEP.12 = c(73L, 2L, 91L, 53L, 288L, 
6L, 2L, 107L, 109L, 97L, 41L, 18L, 12L, 16L, 2L, 2L, 270L, 32L, 
45L, 92L, 54L, 190L, 1L, 4L, 19L, 53L, 1L, 10L, 0L, 61L, 50L, 
27L, 27L, 25L, 3L, 1L, 43L, 0L), newCol = c(56.3063063063063, 
62.2549019607843, 32.5088339222615, 46.218487394958, 57.4712643678161, 
21.3389121338912, 68.75, 76.25, 83.1081081081081, 69.6551724137931, 
58.8235294117647, 30.2325581395349, 56.3106796116505, 45.4935622317597, 
13.0434782608696, 49.1620111731844, 50.4424778761062, 44.4444444444444, 
56.0402684563758, 57.6687116564417, 20.9302325581395, 61.734693877551, 
31.0077519379845, 58.4905660377358, 63.9175257731959, 83.3333333333333, 
9.9009900990099, 43.2432432432432, 11.8012422360248, 76.4705882352941, 
67.6691729323308, 77.6119402985075, 14.5367412140575, 42.4719101123596, 
10.5590062111801, 62.2222222222222, 44.4444444444444, 3.125)), .Names = c("School", 
"Latitude", "Longitude", "Windows.SEP.11", "Mac.SEP.11", "Windows.SEP.12", 
"Mac.SEP.12", "newCol"), row.names = c(NA, -38L), class = "data.frame")
&#13;
document.body.addEventListener('click', function() {
  if(document.querySelector('[name="select-customer"]:checked') ||
     document.querySelector('[name="choice-selection"]:checked')
    ) {
    document.querySelector('button').className= 'enable';
  }
  else {
    document.querySelector('button').className= 'disable';
  }
});
&#13;
.disable {
  background: gray;
}

.enable {
  background: green;
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

if( $( "input[name='select-customer']" ).is(':checked')) {
      if( $( "input[name='choice-selection']" ).is(':checked')){
               $('#btn').prop('disabled', 'disabled');
                } else {
                     $('#btn').prop('disabled', false);     
                    }
            }
}

试试这个