0

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

2
  • In what way is the checkbox being disabled by JavaScript? Commented Mar 1, 2010 at 17:37
  • 1
    IMO, using a hidden field for checkboxes like this is pointless. Commented Mar 1, 2010 at 17:44

2 Answers 2

2

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;
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Hi Jonathan, your solution looks like mine : stackoverflow.com/questions/1127357/… [But I rendered the checkbox using a inpute element and used jQuery to toggle the valuy)

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.