猿问

如何在更改时更改复选框的值

我想更改复选框的值,但因以下代码而失败。



$("input:checkbox").on("change", function () {

    if (this.checked == true) {

        this.val("1");

    }

    else {

        this.val("0");

    }

});

我不知道为什么没有代码就没有响应,当我调用这个元素时它应该“检查”,但是没有。所以我需要手动添加一个值字段。


<input class="c-switch-input" type="checkbox" name="pk_{{$d['id']}}" value="{{$d['status']}}">


翻过高山走不出你
浏览 81回答 1
1回答

Smart猫小萌

你错误地混合了 jQuery 和 DOM任何一个this.value = "1";或者$(this).val("1")但使用三元:$("input:checkbox").on("change", function () {&nbsp; &nbsp;this.value = this.checked&nbsp; ? "1" : "0";&nbsp; &nbsp;console.log(this.value)});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type="checkbox" />
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答