开发者

Checkbox and Hidden Type issue

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开发者_开发技巧


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;
  });
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜