<input type="checkbox" onclick="onClickHandler()" onchange="onChangeHandler()" />

从onClickHandler和/或onChangeHandler,我怎么能确定什么是复选框的新状态?


当前回答

简单的回答是:

使用click事件,它直到值更新后才会触发,并在你想要它的时候触发:

<label><input type='checkbox' onclick='handleClick(this);'>Checkbox</label>

function handleClick(cb) {
  display("Clicked, new value = " + cb.checked);
}

|源

更长的答案是:

The change event handler isn't called until the checked state has been updated (live example | source), but because (as Tim Büthe points out in the comments) IE doesn't fire the change event until the checkbox loses focus, you don't get the notification proactively. Worse, with IE if you click a label for the checkbox (rather than the checkbox itself) to update it, you can get the impression that you're getting the old value (try it with IE here by clicking the label: live example | source). This is because if the checkbox has focus, clicking the label takes the focus away from it, firing the change event with the old value, and then the click happens setting the new value and setting focus back on the checkbox. Very confusing.

但是如果你用点击来代替,你可以避免所有这些不愉快。

我已经使用了DOM0处理程序(onxyz属性),因为这是你问的,但为了记录,我通常会建议在代码中连接处理程序(DOM2的addEventListener,或旧版本的IE中的attachEvent),而不是使用onxyz属性。这样就可以将多个处理程序附加到同一个元素上,从而避免将所有处理程序都设置为全局函数。


这个答案的早期版本使用了以下代码用于handleClick:

function handleClick(cb) {
  setTimeout(function() {
    display("Clicked, new value = " + cb.checked);
  }, 0);
}

The goal seemed to be to allow the click to complete before looking at the value. As far as I'm aware, there's no reason to do that, and I have no idea why I did. The value is changed before the click handler is called. In fact, the spec is quite clear about that. The version without setTimeout works perfectly well in every browser I've tried (even IE6). I can only assume I was thinking about some other platform where the change isn't done until after the event. In any case, no reason to do that with HTML checkboxes.

其他回答

对于React.js,你可以用可读性更强的代码做到这一点。希望能有所帮助。

handleCheckboxChange(e) {
  console.log('value of checkbox : ', e.target.checked);
}
render() {
  return <input type="checkbox" onChange={this.handleCheckboxChange.bind(this)} />
}

使用这个

<input type="checkbox" onclick="onClickHandler()" id="box" />

<script>
function onClickHandler(){
    var chk=document.getElementById("box").value;

    //use this value

}
</script>
use onclick event on all checkboxes that you want to get their values whether they are checked or not.

<input type="checkbox" value="rightSideCheckbox" onclick='handleClick(this);'>

function handleClick(checkbox) {
    if(checkbox.checked){
        console.log(checkbox.value+"True")
    }
    else{
        console.log(checkbox.value+"False")
    }
}

简单的回答是:

使用click事件,它直到值更新后才会触发,并在你想要它的时候触发:

<label><input type='checkbox' onclick='handleClick(this);'>Checkbox</label>

function handleClick(cb) {
  display("Clicked, new value = " + cb.checked);
}

|源

更长的答案是:

The change event handler isn't called until the checked state has been updated (live example | source), but because (as Tim Büthe points out in the comments) IE doesn't fire the change event until the checkbox loses focus, you don't get the notification proactively. Worse, with IE if you click a label for the checkbox (rather than the checkbox itself) to update it, you can get the impression that you're getting the old value (try it with IE here by clicking the label: live example | source). This is because if the checkbox has focus, clicking the label takes the focus away from it, firing the change event with the old value, and then the click happens setting the new value and setting focus back on the checkbox. Very confusing.

但是如果你用点击来代替,你可以避免所有这些不愉快。

我已经使用了DOM0处理程序(onxyz属性),因为这是你问的,但为了记录,我通常会建议在代码中连接处理程序(DOM2的addEventListener,或旧版本的IE中的attachEvent),而不是使用onxyz属性。这样就可以将多个处理程序附加到同一个元素上,从而避免将所有处理程序都设置为全局函数。


这个答案的早期版本使用了以下代码用于handleClick:

function handleClick(cb) {
  setTimeout(function() {
    display("Clicked, new value = " + cb.checked);
  }, 0);
}

The goal seemed to be to allow the click to complete before looking at the value. As far as I'm aware, there's no reason to do that, and I have no idea why I did. The value is changed before the click handler is called. In fact, the spec is quite clear about that. The version without setTimeout works perfectly well in every browser I've tried (even IE6). I can only assume I was thinking about some other platform where the change isn't done until after the event. In any case, no reason to do that with HTML checkboxes.

 
const [checkboxval, setCheckboxVal] = React.useState(null);

const handleChangeCheckbox = (e) => {
   setCheckboxVal(e.target.checked);
}


render() {
   return <input type="checkbox" onChange={(e) => handleChangeCheckbox(e)}
}