使用 jquery 验证下拉列表

发布于 2024-12-09 23:25:45 字数 2664 浏览 0 评论 0原文

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

<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--,它应该显示与其他控件一样的错误?

I have a registration form which uses one of jquery plugin for client side validation. I can validate textboxes and other controls but I have a problem with dropdownlist.
I have a field on registration page

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

Those dropdown list outputs that

<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>

and I have such implementation on master template

<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>

in jquery file which does validation has such example

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

    };

How could I change the code so if visitor leaves the option --Choose--, it should display the error like other controls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-12-16 23:25:45

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

或者也许:

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

if($('#ddlSector').selectedIndex == 0) might work?

or maybe:

if($("#ddlSector").attr("selectedIndex") == 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文