2013-07-01 5 views
1

checkbox 검사에서 값을 가져 오는 중에 문제가 있습니다. radio button에서 값을 얻을 수 있지만 체크 박스에서 값을 가져올 수 없습니다. 당신이 jQuery를 사용하려는 경우자바 스크립트를 사용하여 체크 박스에서 값 가져 오기

코드

<html> 
<head> 
    <script language="javascript"> 

     function subscription() { 
      var subamount = document.querySelector('input[name="sublvl"]:checked').value; 
      $('input:checked').each(function() { 
       // To pass this value to its nearby hidden input 
       $(this).parent('td').next('input').val($(this).val()); 
      }); 

      alert(subamount); 
     } 

    </script> 
</head> 
<body> 
    <h3> 
     <span style="color: #ff0000;">Step 1: Choose your donation amount</span></h3> 
    <form name="pclvl" action="#" method="get"> 
    <label> 
     <input type="radio" name="sublvl" value="10" /> 
     Pathfinder Chronicler Supporter $10.00 USD</label> 
    <br> 
    <label> 
     <input type="radio" name="sublvl" value="15" /> 
     Pathfinder Chronicler Bronze Supporter $15.00 USD</label> 
    <br> 
    <label> 
     <input type="radio" name="sublvl" value="20" /> 
     Pathfinder Chronicler Silver Supporter $20.00 USD</label> 
    <br> 
    <label> 
     <input type="radio" name="sublvl" value="30" /> 
     Pathfinder Chronicler Gold Supporter $30.00 USD</label> 
    <br> 
    <label> 
     <input type="radio" name="sublvl" value="40" /> 
     Pathfinder Chronicler Platinum Supporter $40.00 USD</label> 
    <br> 
    <label> 
     <input type="radio" name="sublvl" value="45" /> 
     Pathfinder Chronicler Jewel Supporter $45.00 USD</label> 
    <br> 
    <label> 
     <input type="radio" name="sublvl" value="50" /> 
     Pathfinder Chronicler Diamond Supporter $50.00 USD</label> 
    <br> 
    </p> 
    </form> 
    <h3> 
     <span style="color: #ff0000;">Step 2: Choose your donation reward</span></h3> 
    <p> 
     <form name="donations" action="#" method="get"> 
     <input type="checkbox" name="reward" value="1" /> 
     Vol. I Package ($10 value) A print copy of Pathfinder Chronicler Volume I (shipping 
     charges not included)<br /> 
     <input type="checkbox" name="reward" value="2" /> 
     Vol. II Package ($10 value) A print copy of Pathfinder Chronicler Volume II (shipping 
     charges not included) 
     <br /> 
     <input type="checkbox" name="reward" value="3" /> 
     Vol. III Package ($10 value) A print copy of Pathfinder Chronicler Volume III (shipping 
     charges not included) 
     <br /> 
     <input type="checkbox" name="reward" value="4" /> 
     Vol. I Package + Vol. I Poster ($15 value) A print copy of Pathfinder Chronicler 
     Volume I with a 11x17 Poster of Volume I Source Art by Eva Widermann (shipping charges 
     not included)<br /> 
     <input type="checkbox" name="reward" value="5" /> 
     Vol. II Package + Vol. II Poster ($15 value) A print copy of Pathfinder Chronicler 
     Volume II with a 11x17 Poster of Volume II Source Art by Carolina Eade (shipping 
     charges not included)<br /> 
     <input type="checkbox" name="reward" value="6" /> 
     Vol. III Package + Vol. III Poster ($15 value) A print copy of Pathfinder Chronicler 
     Volume II with a 11x17 Poster of Volume III Source Art by Carolina Eade (shipping 
     charges not included)<br /> 
     <input type="checkbox" name="reward" value="7" /> 
     Complete Package ($50 value) Print copies of Pathfinder Chronicler Volume I, II 
     and III with 11x17 Posters of Volume I, II, III, and IV Source Art, by Eva Widermann 
     and Carolina Eade (shipping charges not included)<br /> 
     <input type="checkbox" name="poster" value="poster" /> 
     Poster Package ($20 value) 11x17 Posters of Volume I, II, III, and IV Source Art, 
     by Eva Widermann and Carolina Eade (shipping charges not included)<br /> 
    </p> 
    <input type="button" value="test click" onclick="subscription(this.form)"> 
    </form> 
</body> 
</html> 
+0

'다음 내용 ('입력')'아마'다음 내용 ('입력 : 숨겨진')이어야한다 확인란을 얻기 위해 변경'. 그런데 어째서 그것을 복제하고 있습니까? 귀하의 확인란이 제출되지 않았습니까? – Bergi

+0

'보상'의 여러 선택을 어떻게 처리 할 계획입니까? – Bergi

+2

jQuery * 및 * 네이티브 DOM 선택기를 모두 사용하는 이유는 무엇입니까? – Bergi

답변

1

뿐만 아니라 jQuery를에 충실 할 수 있습니다. 나는 당신이 잠시 동안 웹 프로그래밍을 떠나 있었기 때문에 많은 의견을 포함하고 있으며, 리프레셔를 사용할 수도 있습니다.

편집 : 그냥, 당신의 편집을 보았다

function subscription() { 

    // Create an array of checkbox inputs that are checked 
    var $inputs = $('input[type="checkbox"]:checked'); 

    // Iterate through all the checked inputs, you could use 
    // jQuery's $each here but it's horribly slow 
    for (var i = 0; i < $inputs.length; i++) { 

     // Assign the value 
     var inputValue = $($inputs[i]).val(); 

     // Please note I cannot see the hidden field on your markup, but let's 
     // just target the first hidden field available if that is what you want 
     // If you include the hidden field on the markup, we could target it 
     // specifically also... 
     $('input:hidden').val(inputValue); 

    } 
} 
+0

그러면 어떻게 출력합니까? –

+0

그래, 내가 물어 보자 내가 라디오 버튼과 체크 박스에서 정보를 얻고 페이지를 떠나면서 이것을 할 수있는 가장 좋은 방법은 페이팔로 보내야합니다. –

관련 문제