使用jquery验证下拉列表

时间:2011-10-14 08:25:31

标签: jquery-plugins drop-down-menu validation uniform

我有一个注册表单,它使用jquery插件之一进行客户端验证。我可以验证文本框和其他控件,但我有dropdownlist的问题。 我在注册页面上有一个字段

<asp:DropDownList ID="ddlSector" runat="server" class="validateCallback my_code_callback"></asp:DropDownList>

那些下拉列表输出

<select name="ddlSector" class="validateCallback my_code_callback">
   <option value="244">--Choose--</option>
   <option value="27">Hotel</option>
   <option value="28">Motel</option>
   <option value="29">Cottage</option>
</select>

我在主模板上有这样的实现

<script type="text/javascript" src="/scripts/uni-form-validation.jquery.js" charset="utf-8"></script>
    <script type="text/javascript">
        $(function () {
            $('form.uniForm').uniform({
                prevent_submit: true
            });              
          });  
          window.my_code_callback = function (field, caption) {
          if (field.val() === '244') {
              return false;
            }
          }    
    </script>
在jquery文件中

进行验证有这样的例子

 /**
         * Callback validator
         *
         * Lets you define your own validators. Usage:
         *
         * <input name="myinput" class="validateCallback my_callback" />
         *
         * This will result in UniForm searching for window.my_callback funciton and
         * executing it with field and caption arguments. Sample implementation:
         *
         * window.my_callback = function (field, caption) {
         *   if (field.val() === '34') {
         *     return true;
         *   } else {
         *     return caption + ' value should be "34"';
         *   }
         * }
         *
         * @param jQuery field
         * @param caption
         */
        validateCallback : function (field, caption) {
            var classes = field.attr('class').split(' '),
                callback_function = '';

            for (var i = 0; i < classes.length; i += 1) {
                if (classes[i] === 'validateCallback') {
                    if (classes[i + 1] != 'undefined') {
                        callback_function = classes[i + 1];
                        break;
                    }
                }
            }

            if (window[callback_function] != 'undefined' && (typeof window[callback_function] === 'function')) {
                return window[callback_function](field, caption);
            }

            return i18n('callback', caption, callback_function);
        }

    };

我如何更改代码,以便访问者离开选项-Choose--时,它应该像其他控件一样显示错误?

1 个答案:

答案 0 :(得分:0)

if($('#ddlSector').selectedIndex == 0)可能有用吗?

或者也许:

if($("#ddlSector").attr("selectedIndex") == 0)