admin管理员组

文章数量:1352788

Am I doing this wrong or is this impossible? I want to set the value of the checkbox to "0" if unchecked, and "1" if checked

<input type="checkbox" onChange="function() {$(this).val(this.checked? '1': '0')};" />

NOTE: I know how to do it outside of the onChange event or by doing onChange="takeCareOfThis()", I'm looking to do it all inline

Am I doing this wrong or is this impossible? I want to set the value of the checkbox to "0" if unchecked, and "1" if checked

<input type="checkbox" onChange="function() {$(this).val(this.checked? '1': '0')};" />

NOTE: I know how to do it outside of the onChange event or by doing onChange="takeCareOfThis()", I'm looking to do it all inline

Share Improve this question asked Apr 25, 2016 at 8:20 NaturalBornCamperNaturalBornCamper 3,8866 gold badges43 silver badges63 bronze badges 3
  • <input type="checkbox" onChange="$(this).val(this.checked? '1': '0');" /> – Satpal Commented Apr 25, 2016 at 8:21
  • Don't put the logic in a function block. However, the point of setting the value if the element is checked or not seems pletely redundant. – Rory McCrossan Commented Apr 25, 2016 at 8:21
  • I hear you @RoryMcCrossan, it would take too long to explain why, but I do need it here – NaturalBornCamper Commented Apr 25, 2016 at 8:36
Add a ment  | 

1 Answer 1

Reset to default 7

You just need to set the statement to be executed.

<input type="checkbox" onChange="$(this).val(this.checked? '1': '0');" />

本文标签: javascriptChange the value of a checkbox inline using the onChange attributeStack Overflow