4
关注
2116
浏览

jQuery检测是否在加载时选择了单选按钮

为什么被折叠? 0 个回复被折叠
speedboy007 未验证用户 用户来自于: 广东省
2020-09-18 01:52
是的,这是很简单的。 创建一个方法并将其作为第一种方法调用,以检查单选按钮的状态是否为选中状态,然后添加类。这是因为你刚刚处理了单选按钮onChange()中的逻辑,所以你没有刷新所需的结果。 $(document).ready(function() { checkRadioButtonStatus(); function checkRadioButtonStatus() { if($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) { $("#salary").removeClass('disabled'); } else { $("#salary").addClass('disabled'); } } });
习惯孤独 未验证用户 用户来自于: 广东省
2020-09-17 02:15

只是触发您change处理您的页面完成后的.trigger()功能加载:

$("input[type=radio]").change(function() { 
 
    if ($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) { 
 
    $("#salary").removeClass('disabled'); 
 
    } else { 
 
    $("#salary").addClass('disabled'); 
 
    } 
 
}); 
 

 
$(function(){ 
 
    $("input[type=radio]").trigger('change'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">script> 
 
<input class="SelectMarital" id="marital1" type="radio" name="marital" value="1" checked/> 
 
<input class="SelectMarital" id="marital2" type="radio" name="marital" value="2" /> 
 

 
<input class="SelectPrice" id="price1" type="radio" name="price" value="1" checked/> 
 
<input class="SelectPrice" id="price2" type="radio" name="price" value="2" /> 
 

 
<a href="#" id="salary" class="btn disabled">Next Stepa>

张明 未验证用户 用户来自于: 广东省
2020-09-16 09:02

你可以定义检查函数然后调用它在就绪功能,并且还当你连接喜欢你change事件:

$(function(){ 
    //First call for the load 
    checkRadioButtons(); 

    //Second call for change event 
    $("input[type=radio]").change(checkRadioButtons); 
}); 

$(function() { 
 
    checkRadioButtons(); 
 
    $("input[type=radio]").change(checkRadioButtons); 
 
}); 
 

 
var checkRadioButtons = function() { 
 
    if ($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) { 
 
    $("#salary").removeClass('disabled'); 
 
    } else { 
 
    $("#salary").addClass('disabled'); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">script> 
 

 
<input class="SelectMarital" id="marital1" type="radio" name="marital" value="1" checked/> Marital 1 
 
<input class="SelectMarital" id="marital1" type="radio" name="marital" value="2" /> Marital 2 
 
<br> 
 
<input class="SelectPrice" id="price1" type="radio" name="price" value="1" checked/> Price 1 
 
<input class="SelectPrice" id="price2" type="radio" name="price" value="2" /> Price 2 
 
<br><br> 
 
<a href="#" id="salary" class="btn disabled">Next Stepa>

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 16:04
关注人数
4 人关注
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号