반응형
jQuery를 사용하여 확인란 변경을 처리하는 방법은 무엇입니까?
코드가 몇 개 있습니다.
<input type="checkbox" id="chk" value="value" />
<label for="chk">Value </label>
<br/>
<input type="button" id="But1" value="set value" />
<br />
<input type="button" id="But2" value="read checked" />
Javascript:
$(document).ready(function () {
console.log("Ready ...");
registerHandlers();
function registerHandlers() {
$('#But1').click(function () {
$('#chk').prop('checked', !$('#chk').is(':checked'));
});
$('#But2').click(function () {
var chk1 = $('#chk').is(':checked');
console.log("Value : " + chk1);
});
$('input[type="checkbox"]').change(function () {
var name = $(this).val();
var check = $(this).prop('checked');
console.log("Change: " + name + " to " + check);
});
}
});
jQuery를 사용하여 확인란 변경을 처리하는 방법은 무엇입니까?확인란이 선택된 경우 핸들러를 변경해야 합니다.
[업데이트]
확인란과 몇 개의 버튼이 있습니다.각 버튼은 확인란을 변경할 수 있습니다.확인란을 변경하는 이벤트를 잡는 방법은 무엇입니까?
[업데이트]
이 예제 jsfiddle에 핸들 변경 확인란이 필요합니다.상자를 클릭하면 메시지 "확인" 버튼이 표시되지 않습니다.
선택기 사용:
$(':checkbox').change(function() {
// do stuff here. It will fire on any checkbox change
});
코드: http://jsfiddle.net/s6fe9/
필드의 ID도 사용할 수 있습니다.
$('#checkbox1').change(function() {
if($(this).is(":checked")) {
//'checked' event code
return;
}
//'unchecked' event code
});
호프, 이게 도움이 될 거예요.
$('input[type=checkbox]').change(function () {
if ($(this).prop("checked")) {
//do the stuff that you would do when 'checked'
return;
}
//Here do the stuff you want to do when 'unchecked'
});
$("input[type=checkbox]").on("change", function() {
if (this.checked) {
//do your stuff
}
});
$('#myForm').on('change', 'input[type=checkbox]', function() {
this.checked ? this.value = 'apple' : this.value = 'pineapple';
});
RemoveProp이 Chrome에서 제대로 작동하지 않는 것 같습니다. jsfiddle
$('#badBut1').click(function () {
checkit('Before');
if( $('#chk').prop('checked') )
{
$('#chk').removeProp('checked');
}else{
$('#chk').prop('checked', true);
}
checkit('After');
});
$('#But1').click(function () {
checkit('Before');
if( $('#chk').prop('checked') )
{
$('#chk').removeClass('checked').prop('checked',false);
}else{
$('#chk').addClass('checked').prop('checked', true);
}
checkit('After');
});
$('#But2').click(function () {
var chk1 = $('#chk').is(':checked');
console.log("Value : " + chk1);
});
$('#chk').on( 'change',function () {
checkit('Result');
});
function checkit(moment) {
var chk1 = $('#chk').is(':checked');
console.log(moment+", value = " + chk1);
};
이름으로 라디오 값 가져오기
$('input').on('className', function(event){
console.log($(this).attr('name'));
if($(this).attr('name') == "worker")
{
resetAll();
}
});
언급URL : https://stackoverflow.com/questions/9180087/how-to-handle-change-of-checkbox-using-jquery
반응형
'programing' 카테고리의 다른 글
Javascript를 사용한 함수 호출로 포스트 폼 제출 시뮬레이션 (0) | 2023.08.04 |
---|---|
JQuery 타이머를 사용하여 js-function 호출 (0) | 2023.08.04 |
빈 JQuery 개체 가져오기 (0) | 2023.08.04 |
ViewStateMode와 EnableViewState 비교 (0) | 2023.08.04 |
asp.net : 잘못된 포스트백 또는 콜백 인수 (0) | 2023.08.04 |