2017-12-27 7 views
2

次のステップのボタンから無効なクラスを削除します。両方のラジオボタンがオンになっていれば正常ですが、ページをリロードするとラジオボタンはチェックされたままです次のステップで無効になったクラスはトリガされません。それを作業負荷にするためにとにかくありますか?ロード時にラジオボタンが選択されているとjQueryが検出する

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

誰でも手助けできますか?

+0

ラジオボタンの1つを選択して設定します。 – Zain

答えて

1

あなたはあなたのようなあなたのchangeイベントを添付するときにも、レディ機能でそれを呼び出し、チェック機能を定義することができます。私はPHPを使用して

$(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 Step</a>

1

あなたのページには、.trigger()機能とロードが終了した後、ちょうどあなたのchangeハンドラをトリガ:はい、それは非常に簡単です

$("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 Step</a>

0

メソッドを作成し、ラジオボタンの状態をチェックしたかどうかを確認する最初のメソッドとして呼び出し、クラスを追加します。その理由は、onChange()のラジオボタン内のロジックを処理したばかりのときに、リフレッシュしたい結果が得られないからです。

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

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