Checkbox ignores value attribute when checked #4018
Replies: 6 comments 8 replies
-
What is the use case for a singular checkbox with a value? Wouldn't I think just changing the behavior wholesale may be a breaking change (even when only applied if Though, I'm honestly struggling to see the use case for it... You could just make the thing it's modeled to be an array... |
Beta Was this translation helpful? Give feedback.
-
I'm converting a form used to edit/update a MySQL table. One of the columns is a When a record is retrieved (using a new API), it has 1 or 0 in that column, and I am trying to use that value to model the checkbox. I think the answer will be to convert the 1/0 to true/false as soon as it's received at the front-end; model the checkbox on that value; and convert back when the form data is prepared for submission. I'll see how it goes. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your replies. I realise after all this, that I'm overthinking it. I don't need to I can just use the value from the API response to set the checked attribute, and then use the normal form checkbox behaviour. If the box is checked, it is submitted with the value 1. If it's not checked, then it isn't submitted at all, and the server supplies a default value.
|
Beta Was this translation helpful? Give feedback.
-
For info - I've settled on this approach, which keeps things simple and in sync.
It means that I can use my The |
Beta Was this translation helpful? Give feedback.
-
I know, I'm late for the game, but you're using the wrong control. The first responder was right: Checkboxes are either for true or false (boolean) values or for selecting multiple values. Both scenarios are covered by Alpinejs. In your case, you want to select a specific value. That's what So here's my solution for you: <label><input type="radio" x-model="api.publish" value="1">published</label>
<label><input type="radio" x-model="api.publish" value="0">unpublished</label> (You can still add |
Beta Was this translation helpful? Give feedback.
-
The only thing that worked for me was: <input type="checkbox"
x-init="$el.value = myValue"
x-bind:checked="myValue"
x-on:change="$el.value = $el.checked"
/></td> This is very counter intuitive and complicated. I've been using alpinejs for a while now and always stumble over checkboxes. There should be a new subsection in the docs explaining how to deal with single and multiple checkboxes. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It seems that a checkbox ignores the HTML value attribute, and just sets the x-model variable to true or false.
Here's a page that demonstrates the issue. Note how when you tick/untick the first box, it changes to true, then false, rather than toggling between '1' and false (or null).
By comparison, the second two checkboxes (in an array) do use their value attributes.
This is inconsistent behaviour and it doesn't respect the usual way that HTML form fields are handled. What am I doing wrong - or do I just have to accept that this is how it's going to be?
Beta Was this translation helpful? Give feedback.
All reactions