tags:

views:

48

answers:

2

All,

I have a checkbox and a hidden variable with the same name.

<input name="chk9[143]" value="" type="hidden">
<input name="chk9[143]" onclick="toggleParentCheckboxes(this, 'frmSomething', 0);" type="checkbox">

When the form is posted, I get the value of the hidden variable even if the checkbox is checked or not. That is fine. Now, if the checkbox is disabled through JavaScript, is it possible to change the hidden element value to "disabled" through JQuery? So, when the form is posted, instead of getting the value as "", I should get the value as "disabled".

Thanks

+1  A: 

On the submit action of the form, check to see if the checkbox is not checked. If it's not checked, change the value of the hidden element to "disabled":

$(".myform").submit(function(){
  $(":hidden[name='chk9[143]']")
    .val( $(":checkbox[name='chk9[143]']").is(":checked") ? "" : "disabled" );
});

This example assumes that you are dealing with only two explicit elements, and not multiple sets of pairs.

Jonathan Sampson
Hi Jonathan, your solution looks like mine : http://stackoverflow.com/questions/1127357/why-does-asp-net-mvc-html-checkbox-output-two-inputs-with-the-same-name/3718055#3718055[But I rendered the checkbox using a inpute element and used jQuery to toggle the valuy)
Stef
+2  A: 

You mean like getting the checkboxes, looking for a matching hidden field, and set the value according to whether the checkbox is disabled:

$(function() {
  $('form').submit(function(){
    $('input[type=checkbox]').each(function(){
      $('input[type=hidden][name=\''+this.name+'\']').val(this.disabled ? 'disabled' : '');
    });
    return true;
  });
});
Guffa