>  기사  >  웹 프론트엔드  >  Jquery_jquery의 CheckBox, RadioButton 및 DropDownList에 대한 값 할당 구현 코드

Jquery_jquery의 CheckBox, RadioButton 및 DropDownList에 대한 값 할당 구현 코드

WBOY
WBOY원래의
2016-05-16 18:00:56939검색

Jquery 버전이 매우 빠르게 업데이트되면서 코드 작성 방식도 많이 바뀌었습니다. 다음 Jquery 코드는 query1.4 이상
Radio
에 적합합니다.
1. 선택한 값을 가져옵니다. 세 가지 방법을 사용할 수 있습니다.
$('input:radio:checked').val()
$("input[type='radio']:checked" ).val();
$("input[name='rd']:checked").val()
2. 첫 번째 라디오를 선택한 값으로 설정합니다:
$('input :radio:first').attr( 'checked', 'checked');
또는
$('input:radio:first').attr('checked', 'true'); 참고: attr("checked",' selected')= attr("checked", 'true')= attr("checked", true)
3. 마지막 라디오를 선택한 값으로 설정합니다:
$ ('input:radio:last'). attr('checked', 'checked');
또는
$('input:radio:last').attr('checked', 'true');
4. 인덱스 값을 기반으로 임의의 값 설정 라디오가 선택된 값입니다:
$('input:radio').eq(index value).attr('checked', 'true'); value=0,1,2....
또는
$('input:radio').slice(1,2).attr('checked', 'true')
5. 값 값
$(" input:radio[value='rd2']").attr('checked','true')에 따라 라디오를 선택한 값으로 설정합니다.
또는
$( "input[value='rd2']").attr('checked' ,'true');
6. 값이 rd2인 라디오 삭제
$("input:radio[value='rd2' ]").remove();
7. 라디오 번호 삭제
$("input:radio").eq(index value).remove(); index value=0,1,2... .
세 번째 Radio를 삭제하는 경우:$("input: radio").eq(2).remove()
8. Traverse Radio
$('input:radio').each( function(index,domEle){
//코드 작성
})

DropDownList

1. 🎜>선택한 항목의 값 가져오기: $('select #sel option:selected').val()
또는
$('select#sel').find('option: selected').val();
선택한 항목의 텍스트 값 가져오기 :
$('select#seloption:selected').text()
또는
$('select# sel').find('option:selected').text();
2. 현재 선택된 항목의 인덱스 값을 가져옵니다:
$('select#sel').get(0).selectedIndex ;
3. 현재 옵션의 최대 인덱스 값을 가져옵니다:
$('select #sel option:last').attr("index")
4 DropdownList의 길이를 가져옵니다. >$('select#sel')[0].options.length;
또는
$('select#sel').get(0).options.length
5. 옵션을 선택한 값으로:
$('select#sel option:first').attr(' selected','true')
또는
$('select#sel')[0]. selectedIndex = 0;
6. 마지막 옵션을 선택한 값으로 설정합니다:
$('select #sel option:last).attr('selected','true')
7. 인덱스 값을 기준으로 선택한 값으로:
$('select#sel')[0].selectedIndex = 인덱스 값=0,1,2....
8. 선택한 값으로 Value=4 사용:
$('select#sel').attr('value','4 ')
또는
$("select#sel option[value=' 4']").attr('selected', 'true');
9. 값=3인 옵션을 삭제합니다:
$("select#sel option[value='3']"). 제거();
10. 처음 몇 가지 옵션을 삭제합니다:
$(" select#sel option ").eq( 인덱스 값).remove(); 인덱스 값=0,1,2.
세 번째 라디오를 삭제하려는 경우:
$(" select#sel option ").eq(2).remove( )
11. 첫 번째 옵션 삭제:
$(" select#sel option ").eq(0).remove();
또는
$("select#sel option: first").remove();
12. 마지막 옵션 삭제:
$("select#sel option:last").remove();
13. 드롭다운 목록 삭제:
$( "select#sel").remove(); 선택 후:
$("select#sel").append("
f
")
15 선택 앞에 옵션 추가:
$("select#sel ").prepend("");
16. 트래버스 옵션:
$(' select #sel option ').each(function (index, domEle) { })

CheckBox



1. 단일 체크박스 선택 항목 가져오기(3가지 쓰기 방법): $("input:checkbox:checked").val() 또는 $("input:[type='checkbox']:checked").val();

또는
$("input:[name='ck']:checked").val( ) ;
2. 선택된 여러 항목 가져오기:
$('input:checkbox').each(function() {
if ($(this).attr('checked') ==true ) {
alert($(this).val())
}
});
3. 첫 번째 체크박스를 선택한 값으로 설정합니다:
$('input:checkbox:first').attr("checked",'checked'); 'input:checkbox').eq(0).attr("checked",'true')
4. 마지막 체크박스를 선택한 값으로 설정합니다:
$('input:radio:last') . attr('checked', 'checked');
또는
$('input:radio:last').attr('checked', 'true')
5. 인덱스 값에서 체크박스는 선택된 값입니다:
$('input:checkbox).eq(index value).attr('checked', 'true'); .
또는
$('input:radio').slice(1,2).attr('checked', 'true')
6. 여러 확인란을 선택합니다.
첫 번째 체크박스와 동시에 2개의 체크박스:
$('input:radio').slice(0,2).attr('checked','true')
7. Value 값에 따라 선택된 값:
$("input:checkbox[value='1']").attr('checked','true')
8. :
$("input:checkbox [value='1']").remove()
9. 어떤 체크박스를 삭제하세요:
$("input:checkbox").eq(index value) .remove(); 인덱스 값= 0,1,2....
세 번째 체크박스를 삭제하려면:
$("input:checkbox").eq(2).remove(); 10. 확인란을 탐색합니다.
$('input:checkbox').each(function (index, domEle) {
//코드 작성
})
모두 선택
$('input:checkbox' ).each(function() {
$(this).attr('checked', true);
})
모두 선택 해제:
$ ('input:checkbox' ).each(function () {
$(this).attr('checked',false);
});

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.