2013-07-09 3 views
1

나는 각 <td> 요소 내에 숨겨진 필드를 채우려면 다음과 같은 HTML 및 jQuery를 한 다음jQuery를이 - 라디오의 값을 가장 가까운 숨겨진 입력을 채 웁니다

<td> 
    <div id="sex_div" style="text-align: left; width:120;"> 
     <input type="hidden" id="sex" value=""/> 
     <input type="radio" id="sex_male" name="sex_div" value="M"/><label for="sex_male">Male</label> 
     <input type="radio" id="sex_female" name="sex_div" value="F"/><label for="sex_female">Female</label> 
    </div> 
</td> 

JQuery와 내가 가지고있는이다과 같이

$("input :radio").click(function() { 
    $(this).siblings("input [type='hidden']").val($(this).val()); 
}); 

그리고,

$("#sex_div").buttonset(); 

이 전체 FO의 단지 작은 부분 buttonset이다 분명히 rm. 나머지는 모두 비슷하게 보입니다. 이제 라디오 버튼을 클릭하거나 선택할 때 숨김 필드가 설정되지 않는 것이 문제입니다. 나는 2 일 동안이 겉으로보기에는 쉬운 문제로 지금 고투하고있다!

도움 주셔서 감사합니다.

답변

5
$(this).siblings("input[type='hidden']").val($(this).val()); 

의 공간을 제거해야합니다. 선택자의 공백은 다음 토큰과 일치하는 하위 요소를 검색하는 것을 의미합니다.

+0

공간 ... 진심으로! 고마워요. 공간이 완전히 다른 것을 의미한다는 것을 결코 알지 못했습니다! – iLikeBreakfast

1

당신은 input[type='hidden'] 사이에는 공백이 없어야합니다 당신의 선택

$("input:radio").click(function() { 
    $(this).siblings("input[type='hidden']").val($(this).val()); 
}); 
관련 문제