2014-04-23 2 views
0

확인란을 선택하지 않은 경우 제출 버튼을 비활성화하는 데 문제가 있습니다. 그것은 체크 박스를 제외하고 다른 모든 것을 위해 작동합니다.확인란을 선택 취소하면 비활성화 됨

<fieldset> 
    <input class="disableButton" id="card" type="text" /> 
    <select class="selected-state disableButton" id="month" > 
     <option value="">Exp. Month</option> 
     <option value="01">01 - Jan</option> 
     ... 
    </select> 
    <select class="selected-state disableButton" id="year" /> 
     <option value="">Exp. Year</option> 
     <option value="2014">2014</option> 
     ... 
    </select> 
    <input class="disableButton" id="cvc" type="text" /> 
     <p>I agree to the <a href="http://nofusstoothbrush.com/terms-conditions/" target="_blank">Terms and Conditions</a> <input id="termsCheck" type="checkbox" checked></p> 
    <input type="button" name="previous" class="previous action-button" value="Previous" /> 
    <input id="submitCard" class="action-button" type="submit" value="Submit" disabled="disabled" title="Please fill all required fields" /> 
</fieldset>  

$(document).ready(function(){ 
      $('.disableButton').on('keyup change', function(){ 
       if ($('#card').val().length < 10 || $('#cvc').val().length < 3 || $('#month').val() == '' || $('#year').val() == '' || $('#termsCheck').attr('checked') == false){ 
         $('#submitCard').prop('disabled', true); 
       } else { 
         $('#submitCard').prop('disabled', false); 
       } 
      }); 
    }); 
+0

사용을 만들었습니다 생각'대신'attr'의 prop' –

+0

나는 이것을'prop'로 변경했지만 동적으로 작동하지 않습니다. 한 번 작동하지만 두 번 클릭해도 아무런 효과가 없습니다. 어떤 아이디어? –

답변

1

사용 .is(':checked') 또는 .prop('checked') 체크 박스의 상태를 평가하기 :

$(document).ready(function(){ 
    $('.disableButton').on('keyup change', function(){ 
     if ($('#card').val().length < 10 || $('#cvc').val().length < 3 || $('#month').val() == '' || $('#year').val() == '' || !$('#termsCheck').is(':checked')){ 
       $('#submitCard').prop('disabled', true); 
     } else { 
       $('#submitCard').prop('disabled', false); 
     } 
    }); 
}); 

여기서 일하는 예를 참조하십시오 : http://jsfiddle.net/teddyrised/m9KN4/

또한 체크 박스 요소에 클래스 disabledButton을 추가 할 필요가

하고,로를 엄지 손가락의 규칙은 명시 적으로 동의해야하는 사용자 여야합니다. 따라서 의 선택을 취소하는 것이 좋습니다. :

<input id="termsCheck" type="checkbox" class="disableButton" /> 

P/S를 : 나는 당신이 실수로 <select> 요소의 하나 자동 폐쇄를 ...

+0

고마워요. 지금 상자를 체크하지 않을 때 버튼을 사용할 수 없게되지만 다시 체크 할 때 버튼을 사용할 수 없게됩니다. 그리고'select '에있는 헤드에 대해 감사합니다. –

+0

@ProfessorB 클래스'disableButton'을 체크 박스 요소 또한 사용자가 명시적인 동의를해야하기 때문에 동의 한 기본 체크 상태를 제거하는 것이 좋습니다 ... 피들과 응답이 업데이트되었습니다. – Terry

+0

Duh! 내가 그걸 놓쳤다는 것을 믿을 수 없다 - 감사합니다. –

0
<form> 
    <input class="disableButton" id="card" type="text" /> 
    <select class="selected-state disableButton" id="month" > 
    <option value="">Exp. Month</option> 
    <option value="01">01 - Jan</option> 
    ... 
    </select> 
    <select class="selected-state disableButton" id="year" /> 
    <option value="">Exp. Year</option> 
    <option value="2014">2014</option> 
    ... 
    </select> 
    <input class="disableButton" id="cvc" type="text" /> 
    <p>I agree to the <a href="http://nofusstoothbrush.com/terms-conditions/" target="_blank">Terms and Conditions</a> <input id="termsCheck" type="checkbox" checked></p> 
    <input type="button" name="previous" class="previous action-button" value="Previous" /> 
    <input id="submitCard" class="action-button" type="submit" value="Submit" disabled="disabled" title="Please fill all required fields" /> 
</form>  


$(document).ready(function(){ 
    $('form').on('keyup change', function(){ 
    if ($('#card').val().length < 10 || $('#cvc').val().length < 3 || $('#month').val() == '' || $('#year').val() == '' || $('#termsCheck').prop('checked') == false){ 
     $('#submitCard').prop('disabled', 'disabled'); 
    } else { 
     $('#submitCard').prop('disabled', ''); 
    } 
    }); 
}); 

http://jsfiddle.net/622WM/1/

+0

다시 클릭하면 이전 상태로 돌아 가지 않고 버튼을 한 번만 사용 중지하거나 활성화하는 것처럼 보입니다. –

+0

왜? 그것은 모든 양식 keyup 또는 변경 조건을 확인합니다. 코드를 사용해보십시오. – climbinghobo

+0

jsfiddle 예제를 시도하고 있는데 작동하지 않습니다. –

관련 문제