admin管理员组文章数量:1435859
I'm removing the disabled class from Next step button, when both radio button are checked, it's working fine but the problem is when i reload the page,both radio button remain checked but the disabled class on Next step not triggering. Is there anyway to make it work onload?
<input class="SelectMarital" id="marital1" type="radio" name="marital" value="1"/>
<input class="SelectMarital" id="marital2" type="radio" name="marital" value="2"/>
<input class="SelectPrice" id="price1" type="radio" name="price" value="1"/>
<input class="SelectPrice" id="price2" type="radio" name="price" value="2"/>
<a href="#" id="salary" class="btn disabled">Next Step</a>
$("input[type=radio]").change(function(){
if($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) {
$("#salary").removeClass('disabled');
}else {
$("#salary").addClass('disabled');
}
});
Can anyone help?
I'm removing the disabled class from Next step button, when both radio button are checked, it's working fine but the problem is when i reload the page,both radio button remain checked but the disabled class on Next step not triggering. Is there anyway to make it work onload?
<input class="SelectMarital" id="marital1" type="radio" name="marital" value="1"/>
<input class="SelectMarital" id="marital2" type="radio" name="marital" value="2"/>
<input class="SelectPrice" id="price1" type="radio" name="price" value="1"/>
<input class="SelectPrice" id="price2" type="radio" name="price" value="2"/>
<a href="#" id="salary" class="btn disabled">Next Step</a>
$("input[type=radio]").change(function(){
if($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) {
$("#salary").removeClass('disabled');
}else {
$("#salary").addClass('disabled');
}
});
Can anyone help?
Share Improve this question edited Dec 27, 2017 at 15:22 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Dec 27, 2017 at 15:05 ZainZain 6629 silver badges30 bronze badges 1- Using php i'm setting the one of the radio button as selected. – Zain Commented Dec 27, 2017 at 15:11
3 Answers
Reset to default 2You could define the check function then call it in the ready function and also when you attach your change
event like :
$(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./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 Step</a>
Just trigger your change
handler after your page has finished loading with the .trigger()
function :
$("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./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 Step</a>
Yes, it is quite simple.
Make a method and call it as a first method to check the status of radio buttons as checked or not, then add class. The reason is why you are not having a desired result on refresh as you have just handled the logic inside onChange()
of radio buttons.
$(document).ready(function() {
checkRadioButtonStatus();
function checkRadioButtonStatus() {
if($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) {
$("#salary").removeClass('disabled');
}
else {
$("#salary").addClass('disabled');
}
}
});
本文标签: javascriptjQuery detect if radio button is selected on loadStack Overflow
版权声明:本文标题:javascript - jQuery detect if radio button is selected on load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745668324a2669416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论