2016-09-29 2 views
1

특정 값을 체크하면 팝업을 표시하는 내 사이트에 대한 대화 형 체크 박스를 만들려고합니다. 나는 주로 경고 테스트에 체크 박스의 이름을 전달하는 것 같지 않지만 대부분 작동합니다. 내가 사용하는 확인란에 관계없이 항상 question1이 표시됩니다.Jquery가 정확하게 체크 박스 값을 전달하지 않습니다.

<script async src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script> 


function HasChanged() 
{ 
    if($('.common_question').is(":checked")) 
    { 
     alert($('.common_question').val()); 
    } 
} 

</script> 
<div id="chkbox" style="width=75%"> 
<form method="post"> 
     <fieldset> 
       <legend>Select your reason for contact from below.</legend> 
         <h2>Premium Accounts</h2> 
         <input type="checkbox" onchange="HasChanged()" class="common_question" name="questiontype" value="question1" />I want to purchase a premium account<br /> 
         <div style="display:none" id="prem1">Answer is here </div> 
         <input type="checkbox" onchange="HasChanged()" class="common_question" name="questiontype" value="question2" />I want to cancel a premium account<br /> 
         <input type="submit" value="Next" /> 
     </fieldset> 
</form>  
</div> 

나는 잘못 가고있는 곳으로 난처한 상황에 처해있다. 단지 정확한 체크 박스를 표시하기 만하면된다. val()

일치 요소

그것은, 컬렉션의 첫 번째 요소에서 값을 가져옵니다하지 않는 세트의 첫 번째 요소의 현재의 값을 취득하는 일이다

+0

모두 당신의 체크 박스'common_question' 클래스가 액세스 ..... – depperm

답변

2

당신이 바꾼 것뿐입니다. 그런 다음 적절한 이벤트 핸들러를 사용한다
this

<div id="chkbox" style="width=75%"> 
    <form method="post"> 
    <fieldset> 
     <legend>Select your reason for contact from below.</legend> 
     <h2>Premium Accounts</h2> 
     <input type="checkbox" class="common_question" name="questiontype" value="question1" />I want to purchase a premium account 
     <br /> 
     <div style="display:none" id="prem1">Answer is here </div> 
     <input type="checkbox" class="common_question" name="questiontype" value="question2" />I want to cancel a premium account 
     <br /> 
     <input type="submit" value="Next" /> 
    </fieldset> 
    </form> 
</div> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script> 
    $('.common_question').on('change', function() { 
     if (this.checked) alert(this.value); 
    }); 
</script> 

FIDDLE

관련 문제