2014-05-12 5 views
-1

왜 이런 일이 일어나고 있는지 궁금합니다.Jquery : 잘못된 셀 수를 반환하는 선택기

jQuery API 라이브러리의 jQuery :checked 선택기를 사용했습니다. 여기서 흥미로운 점은 클래스의 모든 라디오 버튼을 합격 (합격 또는 불합격) 한 다음 백분율/개수를 표시하도록 추가하는 것입니다. 여러 라디오를 통해 클릭 만 선택한 첫 번째 라디오 버튼에,이 반환되면이 잘 작동 2.

HTML :

<b> 
    <input type="radio" name="storeprestidy" value="pass" class="storepresg" /> 
    <input type="radio" name="storeprestidy" value="fail" class="storepresp" /><br /> 
    <input type="radio" name="storepresclean" value="pass" class="storepresg" /> 
    <input type="radio" name="storepresclean" value="fail" class="storepresp" /><br /> 
    <input type="radio" name="storepreslights" value="pass" class="storepresg" /> 
    <input type="radio" name="storepreslights" value="fail" class="storepresp" /> 
</b> 

자바 스크립트 : 내가 클릭한다면

var countChecked = function() { 
    var sg = $("input.storepresg:checked").length; 
    $("#storepresgtotal").text(sg + (sg === 1)); 
}; 
countChecked(); 
$("input[type=radio]").on("click", countChecked); 

storepresg 클래스의 라디오 버튼이 두 개 이상인 경우 개수가 정확합니다. 잘못된 번호 (2)를 반환하는 첫 번째 선택에서만 나타납니다.

처음으로 포스터를 보내 주셨습니다. 문제가 올바르게 제기 되었기를 바랍니다.

감사합니다.

+0

몰라 당신이있어 무엇을 여기 묻고. 'storepresg' 클래스로만 입력 필드를 선택하고 있기 때문에 두 번째 클래스에는 클래스가 없으므로 첫 번째 입력 만 입력하면됩니다. – CBroe

+0

@CBroe, 실제로 질문을 제출할 때 사전/사전 코드 섹션이 회색으로 표시되어 있지만 생략 될 예정 이었지만 storepresg 및 storepresp와 동일한 클래스로 여러 값을가집니다. 예를 들어 하나의 라디오 버튼 만 보여주었습니다. 내 업데이트 된 예제를 참조하십시오. – user3626810

+1

http://jsfiddle.net/ 또는 이와 유사한 예제를 생성하십시오. – CBroe

답변

0

이 제거

+ (sg === 1) 

를이에서이 같은

$("#storepresgtotal").text(sg); 

을 그리고 행동 :

$("#storepresgtotal").text(sg + (sg === 1)); 

는 다음과 같이해야 http://jsfiddle.net/zdCLm/1/

+0

믿을 수 없습니다. 그렇게 간단했습니다. 고맙습니다! – user3626810