55

I have a MVC3 app using Project Awesome (http://awesome.codeplex.com/), but I am getting a weird behaviour on checkboxes. I have the following simple Html within a Modal popup <input type="checkbox" class="check-box" name="IsDeleted">

When I submit the form containing this element, its post value is 'on' instead of the expected 'true' (when element is checked).

Does anybody know why this is? I am assuming there may be some javascript somewhere messing with the form data, but wanted to check whether there isn't some HTML I am missing.

Thanks

1
  • 1
    I had this when I was using e.target.value, when it should be e.target.checked hope this helps someone. Commented May 3, 2017 at 21:35

11 Answers 11

81

Set the checkboxes value attribute to true and you will get true in your post value.

Sign up to request clarification or add additional context in comments.

4 Comments

This is true if and only if the user checks the box. If not checked nothing gets posted. Also, MVC's binding mechanism will set the value to null if nothing gets posted for that checkbox.
ie: <label><input type="checkbox" id="SomeID" name="SomeName" onclick="CBSelectedValueToTrue(this);" >Weight</label>......function CBSelectedValueToTrue(cb) { if (cb.checked) { cb.value = "true"; } }
Doing this will return true, but never a false value?
It will not return false. If checked, it will return true otherwise, no value.
12

It's browser specific, I suppose, what to send when value is undefined. You need to defined value attribute on your radios/checkboxes to be sure what will be passed back to you. I would suggest value="1"

Comments

9

set data-val="true" and value="true" by deafult... if checkbox is checked then returns true

Comments

2

Check Checkbox is checked or not if checked set Hidden field true else set hidden field false.

$('#hiddenFieldId').val($('#CheckBoxId').attr('checked')=='checked') 

Comments

2

13yrs later. Incase if someone is still having this problem. When validating it use e.target.checked instead of e.target.value. Works everytime!!

Comments

1

Surely you should just check if it is set - the value that it sends across is irrelevant, if it's not checked, then nothing at all gets sent when you POST.

1 Comment

Is that specified in W3C? I thought this was browser-dependent.
0

Nothing worked! I ended up on a hacky way after seeing the serialised form object just before posting to controller/action. Its not safe in case if anyone would have any textboxes inside that may contain ampersands. In my case, i had an array of checkboxes, so I did this hack after I am very sure, i won't have problems.

var formData = $("#form").serialize(); formData = formData.replaceAll('=on&','=true&'); if (formData.endsWith('=on')) { formData = formData.substring(0, formData.length - 3) + "=true"; } 

Hope it helps to those 'someone' with my scenario. Happy hacking.

Comments

0

if you do this :

const checkbox = document.getElementById("myCheckbox"); console.log(checkbox.value); 

you will get the output on instead of true or false

then change to :

const checkbox = document.getElementById("myCheckbox"); console.log(checkbox.checked); 

it will print a boolean value.

Comments

-1

Use jQuery for cross-browser decision. And it will return true or false anyway.

$('#check-box-id').attr('checked' ) == true 

if your checkbox has an id check-box-id. For your current version use the next (select by class name):

$('.check-box').attr('checked' ) == true 

Comments

-2

Use jQuery

var out=$('.check-box').is('checked') 

If checkbox is checked out=true else out=false

Comments

-2

In HTML, add a input type="hidden" above checkbox:

<input name="active" id="activeVal" type="hidden"> <input id="active" type="checkbox"> 

Then, add a script as below:

$('#activeVal').val($('#active').is(':checked')); $('#active').change(function() { $('#activeVal').val($('#active').is(':checked')); }); 

When you do eg. $('#your-form').serialize() in jQuery, you will get value of checkbox when checked active: true or uncheck active: false

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.