Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery检测是否在加载时选择了单选按钮_Javascript_Jquery_Html - Fatal编程技术网

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

Javascript jQuery检测是否在加载时选择了单选按钮,javascript,jquery,html,Javascript,Jquery,Html,我正在从“下一步”按钮删除禁用的类,当两个单选按钮都选中时,它工作正常,但问题是当我重新加载页面时,两个单选按钮都保持选中状态,但下一步禁用的类不会触发。有没有办法让它在加载时工作 <input class="SelectMarital" id="marital1" type="radio" name="marital" value="1"/> <input class="SelectMarital" id="marital2" type="radio" name="mari

我正在从“下一步”按钮删除禁用的类,当两个单选按钮都选中时,它工作正常,但问题是当我重新加载页面时,两个单选按钮都保持选中状态,但下一步禁用的类不会触发。有没有办法让它在加载时工作

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

$(“输入[type=radio]”)。更改(函数(){
如果($('.SelectPrice').is(':checked')&&('.SelectPrice').is(':checked')){
$(“#薪水”).removeClass('disabled');
}否则{
$(“#工资”).addClass(‘禁用’);
}
});

有人能帮忙吗?

只要在页面使用
.trigger()
函数完成加载后触发
更改处理程序即可:

$(“输入[type=radio]”)。更改(函数(){
如果($('.SelectPrice').is(':checked')&&('.SelectPrice').is(':checked')){
$(“#薪水”).removeClass('disabled');
}否则{
$(“#工资”).addClass(‘禁用’);
}
});
$(函数(){
$(“输入[type=radio]”。触发器('change');
});

是的,非常简单

创建一个方法并将其作为第一个方法调用,以检查单选按钮是否处于选中状态,然后添加类。原因是,由于您刚刚处理了单选按钮的
onChange()
中的逻辑,所以刷新时没有得到所需的结果

$(document).ready(function() {
    checkRadioButtonStatus();

    function checkRadioButtonStatus() {
        if($('.SelectMarital').is(':checked') && $('.SelectPrice').is(':checked')) {
            $("#salary").removeClass('disabled');
        }
        else {
            $("#salary").addClass('disabled');
        }
    }
});

您可以定义check函数,然后在ready函数中调用它,也可以在附加
change
事件时调用它,如:

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

    //Second call for change event
    $("input[type=radio]").change( checkRadioButtons ); 
});
$(函数(){
选中单选按钮();
$(“输入[类型=单选]”)。更改(选中单选按钮);
});
var checkRadioButtons=函数(){
如果($('.SelectPrice').is(':checked')&&('.SelectPrice').is(':checked')){
$(“#薪水”).removeClass('disabled');
}否则{
$(“#工资”).addClass(‘禁用’);
}
}

婚姻1
婚姻2

价格1 价格2


使用php,我将单选按钮之一设置为选中状态。