我想更改复选框的值,但因以下代码而失败。$("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']}}">
1 回答

Smart猫小萌
TA贡献1911条经验 获得超7个赞
你错误地混合了 jQuery 和 DOM
任何一个
this.value = "1";
或者
$(this).val("1")
但使用三元:
$("input:checkbox").on("change", function () {
this.value = this.checked ? "1" : "0";
console.log(this.value)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" />
添加回答
举报
0/150
提交
取消