Javascript 输入选中的更改按钮类
当选中单选按钮以及1个或多个复选框时,希望从按钮中删除禁用类并添加启用类Javascript 输入选中的更改按钮类,javascript,css,checkbox,input,radio-button,Javascript,Css,Checkbox,Input,Radio Button,当选中单选按钮以及1个或多个复选框时,希望从按钮中删除禁用类并添加启用类 <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"&
<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>
btn 1
btn 2
选择1
选择2
选择3
选择4
$(文档).ready(函数(){
$(“#按钮”)。单击(函数(){
$('input:radio')。每个(函数(){
var$this=$(this);
if($this.prop('checked')){
$this.closest('span').addClass('enable');
$this.closest('span').removeClass('disable');
}否则{
$this.closest('span').addClass('disable');
$this.closest('span').removeClass('enable');
}
});
$('input:checkbox')。每个(函数(){
var$this=$(this);
if($this.prop('checked')){
$this.closest('span').addClass('enable');
$this.closest('span').removeClass('disable');
}否则{
$this.closest('span').addClass('disable');
$this.closest('span').removeClass('enable');
}
});
});
});代码>
。禁用{
背景:灰色;
}
.启用{
背景:绿色;
}
btn 1
btn 2
选择1
选择2
选择3
选择4
Button
这是一个纯JavaScript函数,它侦听单击事件,然后根据输入的单击状态更改按钮的类:
document.body.addEventListener('click',function()){
if(document.querySelector(“[name=“select customer”]:选中”)||
document.querySelector('[name=“choice selection”]:选中')
) {
document.querySelector('button')。className='enable';
}
否则{
document.querySelector('button')。className='disable';
}
});代码>
。禁用{
背景:灰色;
}
.启用{
背景:绿色;
}
btn 1
btn 2
选择1
选择2
选择3
选择4
按钮
试试这个
if( $( "input[name='select-customer']" ).is(':checked')) {
if( $( "input[name='choice-selection']" ).is(':checked')){
$('#btn').prop('disabled', 'disabled');
} else {
$('#btn').prop('disabled', false);
}
}
}