如何将当前对象作为参数传给onclick的函数

https://img4.mukewang.com/5ba4b292000143ec05960174.jpg

上面的代码没有反应

https://img4.mukewang.com/5ba4b29f0001c37105860159.jpg

这段代码得到想要的效果了


我的问题是如何把第一段代码实现,用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;
    }
}




慕容708150
浏览 993回答 1
1回答

拉风的咖菲猫

this 是关键字,不能作为函数的参数。改成下面这样就好了。function&nbsp;changeValue(that)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(that.checked)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that.value=1; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that.value=0; &nbsp;&nbsp;&nbsp;&nbsp;} }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript