0

I have a checkbox in .net MVC and I need to get the value of this checkbox but it is disabled, this is my code:

<div class="editor-field">
   @Html.CheckBox("cbTratamientoFiscal", New With {.class = "checkbox", .disabled = "disabled"})
</div>

I tried something like this in jquery:

var tratamientoFiscal = $("#cbTratamientoFiscal").val();
// also:
var tratamientoFiscal = $("#cbTratamientoFiscal").is(":checked");
// or:
var tratamientoFiscal = $("#cbTratamientoFiscal").attr('checked');

And nothing is working.

Thanks for your help.

4
  • What is the actual html when it gets rendered? Commented Oct 8, 2015 at 17:55
  • <input class="checkbox" disabled="disabled" id="cbTratamientoFiscal" name="cbTratamientoFiscal" value="true" type="checkbox"><input name="cbTratamientoFiscal" value="false" type="hidden"> Commented Oct 8, 2015 at 18:17
  • You have two elements with the same name, 'cbTratamientoFiscal', bith the checkbox and the hidden inputs. That is likely leading to the trouble. the name attribute selector in the answer below will find both elements. Commented Oct 8, 2015 at 18:30
  • var tratamientoFiscal = $("#cbTratamientoFiscal").is(":checked"); works fine - and here is a fiddle to prove it. (the hidden input has nothing to do with it) Commented Oct 9, 2015 at 0:12

1 Answer 1

3

This will give you a true/false result if the name attribute is 'cbTratamientoFiscal':

$("input[name='cbTratamientoFiscal']").is(':checked')

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

4 Comments

Hello @SamTyson ! when it gets rendered the html is like this: <input class="checkbox" disabled="disabled" id="cbTratamientoFiscal" name="cbTratamientoFiscal" value="true" type="checkbox"><input name="cbTratamientoFiscal" value="false" type="hidden">
$("#cbTratamientoFiscal").is(':checked') should give you true/false since only one of the inputs has that id.
Both appear at the same time, but the value attribute is different, the input hidden always have value="false". Can I try this?: $("input[name='cbTratamientoFiscal', type='chekbox']").is(':checked');
You could also use $("input.checkbox").is(':checked') since you added the checkbox class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.