2009-07-21 8 views
0

jQuery Form plugin을 사용하여 약 4 회의 라디오 선택으로 구성된 양식의 '아약스'제출을하고 있습니다. 그러나 라디오 버튼을 선택하지 않으면 양식이 제출되지 않기를 바란다.jQuery ajaxForm 플러그인을 사용하여 양식을 제출하지 않았습니다.

$(document).ready(function() { 

    var options = { target: '#response', 
        type: 'get', 
        beforeSubmit: ischecked 
        } 

    $('#answer').ajaxForm(options); 

}); 


function ischecked(formData, jqForm, options){ 


    if ($('input:radio', $('#answer').is(':checked'))) 
    { 
      return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

내가 콜백 ischecked 실행지고 있음을 확신 (alert()의가 발광 : 여기

그래서 나는 SO 대답 다른에서 발견 한 코드와 플러그인 문서에 힘 입어 지금까지 내 시도이다 , 등), 코드 자체는 라디오 버튼이 선택되지 않았 음을 감지하지 못하고 양식이 계속 제출됩니다.

여기에 HTML 양식입니다 :

<form action="score.php" id="answer"> 
    <div style="margin-top: 0pt;" class="ans"> 
     <input type="hidden" value="127" name="question_id"/> 
     <input type="hidden" value="f5043d5122cec6eb981c9a2fc7a0a653" name="user_id"/> 
     <input type="hidden" value="36" name="question_key"/> 
     <input type="hidden" value="37" name="next_key"/> 

     <ul id="answers"> 
      <li> 
       <label> 
        <input type="radio" value="464" name="answer_id"/> mod_rewrite       </label> 
      </li> 
      <li> 
       <label> 
        <input type="radio" value="465" name="answer_id"/> XML Site Map       </label> 
      </li> 
      <li> 
       <label> 
        <input type="radio" value="466" name="answer_id"/> Tiny URL       </label> 
      </li> 
      <li> 
       <label> 
        <input type="radio" value="467" name="answer_id"/> both a) and b)       </label> 
      </li> 
      <li> 
       <label> 
        <input type="radio" value="468" name="answer_id"/> a), b) and c) can all be used to make it easier for Google to index your dynamically generated URLs.       </label> 
      </li> 
          </ul> 
    </div> 
    <input type="submit" value="Submit Answer and Get Result" id="answer_submission"/> 
</form> 

답변

2

모든 jQuery를 방법은 일치하는 항목을 찾을 수없는 경우, 그것은 하늘의 배열을 반환, 배열을 반환합니다. '빈'배열을 'if'로 체크하면 true를 반환합니다.

function ischecked(formData, jqForm, options) 
{ 
    return $('input[name="answer_id"]:checked').length > 0; //Checking that the number of checked items are more than zero. 

    //You should also be able to use $('#answers input:checked'). 

} 

편집을 시도해보십시오 저자는 HTML을 게시 한 후 코드를 업데이트했습니다.

+0

가 ($ ('# 답변 : 선택') 길이.) 항상 0입니다, 내가 선택한 라디오 상자 중 하나이 경우에도 마찬가지입니다. – Ian

+0

HTML을 게시 할 수 있습니까? – SolutionYogi

+0

라디오 버튼이 'myGroup'이라는 이름을 사용하여 그룹화 된 경우 $ ('input [name = "myGroup"] : checked)) 길이 – SolutionYogi

2

이 시도 : 내가 경고 할 때

function ischecked(formData, jqForm, options) 
{ 
    return $('input[name=answer]').is('checked'); 
}