由于jquery,由于某些奇怪的原因我收到了“对象预期”错误,并且这不会“提交”表单或将数据输入数据库。

没有jquery,数据可以输入数据库。但现在却没有。

我主要使用jquery来验证asp.net控件。


roosteronacid,验证工作完全正常,id属性也相同。提交只是没有执行服务器端代码,我无法弄清楚是什么问题。

编辑:

这是jquery代码:

 <script type="text/javascript">
        $(document).ready(function() {
            // add custom validation methods
            $.validator.addMethod('phone', function(value, el, params) {
                return this.optional(el) || /^[0-9,+,(), ,]{1,}(,[0-9]+){0,}$/.test(value);
            }, 'Please enter a valid phone number');

            $.validator.addMethod('numbers', function(value, el, params) {
                return this.optional(el) || /^[0-9]+$/.test(value);
            }, 'Invalid entry. Only Numeric is allowed.');


            $.validator.addMethod('domainurl', function(value, el, params) {
                return this.optional(el) || /^(http\:\/\/(?:www\.)?[a-zA-Z0-9]+(?:(?:\-|_)[a-zA-Z0-9]+)*(?:\.[a-zA-Z0-9]+(?:(?:\-|_)[a-zA-Z0-9]+)*)*\.[a-zA-Z]{2,4}(?:\/)?)$/.test(value);
            }, 'Please enter a valid domain url');


            $.validator.addMethod('selectone', function(value, element) {
                return this.optional(element) || (value.indexOf("none") == -1);
            }, 'Please select an option.');



            $("#form1").validate({
                debug: true,
                rules: {
                    txt_name: {
                        required: true,
                        minlength: 2
                    },
                    txt_cmp: {
                        required: true,
                        minlength: 2
                    },
                    txt_tel1: {
                        phone: true,
                        required: true,
                        minlength: 3

                    },
                    txt_tel2: {
                        phone: true,
                        required: false,
                        minlength: 3

                    },
                    txt_mob: {
                        phone: true,
                        required: false,
                        minlength: 9

                    },
                    txt_email: {
                        required: true,
                        email: true
                    },

                    txt_domname: {
                        required: true,
                        domainurl: true
                    },

                    radiobt_domain: "required",

                    ddl_yremail: {
                        required: true,
                        selectone: true
                    },
                    ddl_email: {
                        required: true,
                        selectone: true
                    },

                    txt_space: {
                        required: true,
                        numbers: true

                    },
                    txt_calfr: {
                        required: true
                    },
                    txt_calto: {
                        required: true
                    }  


            },
            messages: {
                txt_name: {
                    required: "This field is required",
                    minLength: "Please enter a valid name"
                },
                txt_cmp: {
                    required: "This field is required",
                    minLength: "Please enter a valid commpany name"
                },
                txt_tel1: {
                    required: "This field is required",
                    minLength: "Please enter a valid telephone number"

                },
                txt_tel2: {
                    minLength: "Please enter a valid telephone number"
                },
                txt_mob: {
                    minLength: "Please enter a valid mobile number"

                },
                txt_email: {
                    email: "Please enter a valid email address",
                    required: "This field is required"
                },

                txt_domname: {
                    required: "This field is required"
                },
                radiobt_domain: "Select the Hosting Type"
            }

        });
    });
    </script>

代码有什么问题吗?

它表示预期在第559行的对象。我检查了jquery.validate.js文件,这是它显示的代码:

addWrapper: function(toToggle) {
            if ( this.settings.wrapper )
                toToggle = toToggle.add( toToggle.parents( this.settings.wrapper ) );
            return toToggle;
        }

jquery代码显示正确位置的所有错误,但一旦更正,它就不会提交数据。

我正在使用的插件:

http://bassistance.de/jquery-plugins/jquery-plugin-验证/

有帮助吗?

解决方案

当您尝试访问未定义,未引用或错误拼写错误的对象时,会发生预期的对象。检查预期的对象。使用Firefox firebug来调试你的javascript或者用IE调试来获取运行时无法找到的对象....

其他提示

我通过正确引用JQuery文件解决了这个问题。我把它放在一个子目录中,没有正确的路径。

在这里回答:提交按钮不会触发服务器端代码

'debug'应该设置为false。

我的猜测是错误在于您使用jQuery验证插件。尝试仅验证一个ASP.NET控件。这将使错误更容易发现:

$("#form1").validate({
    rules: {
       id_of_control_you_know_exists_in_the_rendered_html: {
            required: true,
            minlength: 2
        }
    }
});

另一种可能是您在用户控件中使用ASP.NET控件。在这种情况下,呈现的HTML输入控件的id属性与您在.aspx页面中设置的不同。

我在登台服务器上遇到了同样的问题。比较文件显示它们是相同的,并且在不同站点上托管完全相同的文件没有问题,因此它必须是我们放置文件的特定站点。故障排除后的罪魁祸首是我们在IIS中的网站属性中设置了Footer.html文件设置,因此服务器将其注入到渲染脚本中。因此打破任何良好的兼容代码。 我们关闭了IIS设置中的页脚属性 - 宾果!

我也面临这个问题。但在我的情况下,我是jquery版本prob是存在的。我把最新的版本和它在IE中工作了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top