<input name="auto-backup" id="auto-backup" type="checkbox" onclick="changeValue(this)" />
function changeValue(this) {
if (this.checked) {
$(this).value=1;
} else {
$(this).value=0;
}
}
上面的代码没有反应
<input name="auto-backup" id="auto-backup" type="checkbox" onclick="changeValue()" />
function changeValue() {
if (document.getElementById('auto-backup').checked) {
document.getElementById('auto-backup').value=1;
} else {
document.getElementById('auto-backup').value=0;
}
}
这段代码得到想要的效果了
我的问题是如何把第一段代码实现,用this实现
我直接绑定click函数,下面这段代码搞对了
$("#auto-backup").click(function(){
if (this.checked) {
this.value=1;
} else {
this.value=0;
}
});
下面这段代码搞对了
<input name="auto-backup" id="auto-backup" type="checkbox" onclick="changeValue(this)" />
function changeValue(that) {
if (that.checked) {
that.value=1;
} else {
that.value=0;
}
}
this 是关键字,不能作为函数的参数。改成下面这样就好了。