How to disable checkbox without using disabled?

Asked

Viewed 5,307 times

2

How could I inhibit the alteration of a checkbox unused disabled, given that I want to send it via form.

<input type="checkbox" name="option1" readonly="readonly" checked="checked"/> 
// esta alterando
<input type="checkbox" name="option2" disabled="disabled" checked="checked"/> 
// não envia pelo form

2 answers

4


Not using the default property disabled you can put a onclick.

This happens because when using return false stop the click event that changes the checkbox value.

<input type="checkbox" name="option1" checked="checked" onclick="return false;"/> Teste

Reference: Can HTML checkboxes be set to readonly?

  • 1

    Thanks for sharing your solution, but I found in your reference another solution that for my case in particular suited better. I also shared.

1

You can creatively resolve this using a hidden field.

Suppose you want to disable the following checkbox:

<input type="checkbox" id="chkIdoso" name="chkIdoso" checked />

So you can do:

<!-- Campo oculto que armazena o valor -->
<input type="hidden" id="chkIdoso" name="chkIdoso" value="on" />
<!-- Campo exibido para o usuário -->
<input type="checkbox" id="chkIdosoVisual" name="chkIdosoVisual" checked disabled />

Reference: Can HTML checkboxes be set to readonly?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.