Jquery JavaScript 를 사용하지 않고 할 수 있는 방법은 확인란 체크된다면 / 또는 바닐라?
Javascript:
// Check
document.getElementById("checkbox").checked = true;
// Uncheck
document.getElementById("checkbox").checked = false;
jQuery (1.6+):
// Check
$("#checkbox").prop("checked", true);
// Uncheck
$("#checkbox").prop("checked", false);
jQuery (1.5-):
// Check
$("#checkbox").attr("checked", true);
// Uncheck
$("#checkbox").attr("checked", false);
중요한 아직 mentioned:* 않은 행동
뭘 하신게요? 이 해야했다.
언젠가는 자신을 볼 수 있는 코드를 작성할 경우 의존하는 기술입니까 실패했다. 이벤트 '를 사용할 수 있도록 호출하십시오 화재 ()' 의 확인란 요소점 방법 (예:
document.getElementById('checkbox').click();
그러나 이 상황을 구체적으로 확인란 점검됨 전환합니다 대신 설정하면 'true' 또는 'false'. 그 때 '이벤트' 너희에게베풀어진 변경하십시오 경우에만 볼지옥으로 체크된다면 속성용 실제로 변경.
'속성' () 를 호출하기 전에 점검됩니다 테스트하려면 수 있습니다 ',' 메서드입니다. 예:
function toggle(checked) {
var elm = document.getElementById('checkbox');
if (checked != elm.checked) {
elm.click();
}
}
자세한 내용은 여기를 클릭 방법: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click
<script type="text/javascript">
$(document).ready(function () {
$('.selecctall').click(function (event) {
if (this.checked) {
$('.checkbox1').each(function () {
this.checked = true;
});
} else {
$('.checkbox1').each(function () {
this.checked = false;
});
}
});
});
</script>
예를 들어, 어떤 이유에서인지 너회가 don& # 39, t want to (또는 can& # 39, t) 실행하십시오 확인란 요소 ' ()' 클릭하십시오. 를 통해 직접 해당 값을 변경할 됩니다 오헤크트 속성 ([ui_policytable_java_spe_policy IDL 속성용] [2] 의 '<, 입력 유형 = " > checkbox";;').
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
class ButtonCheck {
constructor() {
let ourCheckBox = null;
this.ourCheckBox = document.querySelector('#checkboxID');
let checkBoxButton = null;
this.checkBoxButton = document.querySelector('#checkboxID+button[aria-label="checkboxID"]');
let checkEvent = new Event('change');
this.checkBoxButton.addEventListener('click', function() {
let checkBox = this.ourCheckBox;
//toggle the checkbox: invert its state!
checkBox.checked = !checkBox.checked;
//let other things know the checkbox changed
checkBox.dispatchEvent(checkEvent);
}.bind(this), true);
this.eventHandler = function(e) {
document.querySelector('.checkboxfeedback').insertAdjacentHTML('beforeend', '<br />Event occurred on checkbox! Type: ' + e.type + ' checkbox state now: ' + this.ourCheckBox.checked);
}
//demonstration: we will see change events regardless of whether the checkbox is clicked or the button
this.ourCheckBox.addEventListener('change', function(e) {
this.eventHandler(e);
}.bind(this), true);
//demonstration: if we bind a click handler only to the checkbox, we only see clicks from the checkbox
this.ourCheckBox.addEventListener('click', function(e) {
this.eventHandler(e);
}.bind(this), true);
}
}
var init = function() {
const checkIt = new ButtonCheck();
}
if (document.readyState != 'loading') {
init;
} else {
document.addEventListener('DOMContentLoaded', init);
}
<input type="checkbox" id="checkboxID" />
<button aria-label="checkboxID">Change the checkbox!</button>
<div class="checkboxfeedback">No changes yet!</div>
끝 - < 스니핏 >;!
모두 확인란과 버튼을 클릭한 후 실행하십시오 경우 어떤 방식으로 어떻게 얻어야 할 수 있습니다.
참고로 난 대해 간략하게 도쿠망스케리셀레스 사용할 수 있지만, 이는 특정 ID / 단순한 쉽게 만들어질 수 있다는 점에서 구성자를 인컨텍스트 디스테이징할 투명지에 전달되는 모든 역할을 하는 경우, 또는 적용하십시오 곷떒 아리아 레이블 확인란 (참고로 난 didn& # 39, 수는 없는 노릇 설정하는 id 버튼 위에 있어야 할 것, 이 경우, 이 방법을 사용하여 확인란 오버클로킹된 아리아 라벨트비 수행됨) 또는 다른 방법으로 이 모든 수를 확대해 나갈 방침이다. 지난 2 '는 단순히 데모 addEventListener 어떻게 작동하잖아.
[2]: # Content_versus_IDL_attributes https://developer.mozilla.org/en-us/docs/web/html/attributes