开发者

jQuery validation plugin - removing elements

开发者 https://www.devze.com 2022-12-23 01:05 出处:网络
I\'m using the jQuery validation plugin. On most of my input type... tags I have class=\'required\'.

I'm using the jQuery validation plugin. On most of my input type... tags I have class='required'.

When I submit the page, via JavaScript, the controls on the page that have this class are found. However, there are a handful of checkboxes that I don't need to validate.

I've tried removing the class code completely from the input tag, also tried class='cancel', and class='required:false.

When doing any of those things though when the form submits it can't find the checkbox control.

How do I still keep the ability to do Request.Form and find m开发者_运维百科y checkbox object but at the same time when the form submits don't apply validation to this particular control.

Thank you.


Edit here.

This is what I'm using without the "checked" code and ternary operator. In my input tag I'm calling a function like this...

sb.Append("     <td><input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' " + strDisabled + " value='true' " + GetPackagingSizeTypeControlValue("chkFlashedCarton") + "  />" + crlf);

Inside that function is where I check for the True or False coming back, like this.

case "chkFlashedCarton":
                strResultValue = pst.FlashedCarton.ToString();
                if (strResultValue == "True")
                {
                    strResultValue = " checked";
                }
                break;

strResultValue is what is returned back.

Does this help to see? Thank you.


I don't think the checkbox not appearing is related to the validation issue. By default, inputs without values are not posted back with the form. One way around that for checkboxes is to have an hidden form field per checkbox that sets the opposite value with the same name as the checkbox. When the form is posted back with the checkbox checked, you'll get both values. If the checkbox isn't checked, you'll get the default value (from the hidden field). Thus, you only need to check if the value for the checkbox contains the non-default value and act appropriately on the server side.

<input type='checkbox' name='cb1' value='true' /> Check Me
<input type='hidden' name='cb1' value='false' />

You can then omit the required class and be assured that you will always get some value for the checkbox.

On the server side, then you do something like:

 bool cb1Flag = false;
 if (Request.Form["cb1"].ToUpper().Contains("TRUE"))
 {
      cb1Flag = true;
 }

Edit (based on your edit)

Try this:

sb.Append("     <td><input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' " + strDisabled + " value='true' " );
if (pst.FlashedCarton)
{
    sb.Append( " checked='checked'" );
}
sb.Append( "  />" + crlf);
0

精彩评论

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