IT/jQuery

radiobox 값 가져오기, 셋팅하기

우서바 2011. 6. 15. 16:15



1. 가져오기

$(":radio[name=field_name]:checked").val();

 

2. 해당값에 체크하기

$(":radio[name=field_name][value='" + field_value+ "']").attr("checked", "checked");


3. 동일이름으로 시작하는 모든 checkbox 에대해 루프돌며 작업하기  

$.each( $('input:checkbox[name^=studymaterialnof]:checked'), function(){

this.checked = false;

});



[ select box 컨트롤 ]

1. select box의 내용 가져오기
$("#select_box > option:selected").val();

== 2012-02-17 추가 ==
최근버전(1.7.x)에서는 그냥 $("#select_box").val(); 해도 나온다.

2. select box의 값 설정
 $("#select_box > option[value=지정값]").attr("selected", "true")

3. select disabled 
$('#sign02_emp_id').attr('disabled', 'true');
해제 $('#sign02_emp_id').attr('disabled', ''); // 'false'를 줬을경우 안됨

4. 선택 했는지 안했는지 확인 방법
if ( ! $("#tbl_divs option:selected").val()) { .... }
또는 if ($("#tbl_divs option:selected").val() == '') { .... }
또는 if ($("#tbl_divs option:selected").val() == 0) { .... }
위에 세가지 모두 확인가능(IE8에서 테스트)

기타 참고 : http://www.texotela.co.uk/code/jquery/select/

[ radio 컨트롤 ]
if($(':radio[name="section"]:checked').length < 1) { alert('분류를 선택하세요'); it.section[0].focus(); return false; } 
또는 if (!$(':radio[name="gender"]:checked').val()) { alert('성별을 선택하세요'); $(':radio[name="gender"]:eq(0)").focus(); return false; } 
if($(':radio[name="p_addressGubun"]:checked').val() != 3) { ... }
체크해제 : $("#office").attr('checked', 'false')가 안되서 it.place[1].checked = false; 로 적용함
disabled : $(':radio[name="gender"]').attr('disabled', 'disabled');
체크 :  $(':radio[name="gender"]:eq(0)').attr('checked', 'checked');
radio 버튼 체크 이벤트 :  $(':radio[name^=answer_select_]').live('click', function() { alert('wtf'); }); // jquery 1.4x 버전부터 지원

[ checkedbox 컨트롤 ]
1. 개수 구하기
$("input[name=chk1]:checkbox:checked").length
☆☆☆  php에서 배열사용시  ☆☆☆
$("input[name='chk1[]']:checked").length

2. 체크여부 확인
$("#check_all").is(':checked')

3.  chk1 개수만큼 돌면서 실행한다
  $("input[name='item[]']:checkbox:checked").each(function(){items.push($(this).val());}); 
  var items_str = items.join(',');
또는 var items_str = items.toString();
또는 var items_str = $("input[name='item[]']:checkbox:checked").map(function () {return this.value;}).get();
체크되어 있는 값들을 : 로 구분지어 가져올때
$("input[name=item[]]:checkbox:checked").map(function () {return this.value;}).get().join(':');
split, join으로 내용을 배열로 만들었다, 문자열로 만들었다 한다.

4. disabled 하기
$("input[name=chk1]").attr('disabled', 'true');

5. 전체선택/해제 함수
 function total_check(obj) {
  if (obj.checked == true) {
   $("input[name='select_file[]']:not(checked)").attr('checked', true); 
  } else {
   $("input[name='select_file[]']:checked").attr('checked', false);
  }
 } // function