对象不支持属性或方法'valid' [英] Object doesn't support property or method 'valid'

查看:403
本文介绍了对象不支持属性或方法'valid'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码抛出以下错误:

My code is throwing the following error:


对象不支持属性或方法'valid'。

Object doesn't support property or method 'valid'.

这是因为当前表单没有验证。那没问题。我想要的是提前检查我是否可以将方法有效应用于当前表单,以避免恼人的JavaScript错误。

This is because the current form has no validation. That is OK. All I want is to check in advance whether or not I can apply the method valid to the current form to avoid the annoying JavaScript error.

这就是我想要的。例如。 if(form.isValidatable())form.valid()

That is all I want. E.g. if(form.isValidatable()) form.valid()

代码没有任何问题。我只需要知道调用方法或属性是否会抛出异常。

There is nothing wrong with the code. All I need is to know whether or not calling a method or property is going to throw an exception.

form.submit(function () {
    if ($(this).valid())
      onFormIsValid($(this));
    else
      onFormIsInvalid($(this));
  });

更新:我是jquery的新手,谢谢你的帮助!!!!!我想要做的是在提交之前和验证之后禁用所有输入。我没有设法调用submitHandler而且很难理解为什么。这就是我正在做这个解决方案的原因。看我的代码。我该如何改进?

UPDATE: I am new to jquery and thank you for your help!!!!! What I am trying to do is to disable all the inputs just before submit and after validation. I do not manage to get the submitHandler being called and it is really hard to realize why. That is why I am doing this solution. See my code. How can I improve this?

我想将它应用于我的解决方案中的所有表格....

I want to apply it to all forms in my solution....

$(document).ready(function () {
  var form = $("form");

  form.bind('invalid-form.validate', function() {
    onFormIsInvalid($(this));
  });

  form.submit(function () {
    if ($(this).valid())
      onFormIsValid($(this));
    else
      onFormIsInvalid($(this));
  });
});

function onFormIsValid(form) {
  $("input", form).prop("readonly", "readonly");
  $("input[type=submit]", form).prop("disabled", true);

  $("input[type=submit]", form).each(function () {
    $(this).data("val", $(this).val());
  });

  $("input[type=submit]", form).val("Processing...");
}

function onFormIsInvalid(form) {
  $("input", form).prop("readonly", null);
  $("input[type=submit]", form).prop("disabled", false);

  $("input[type=submit]", form).each(function () {
    $(this).val($(this).data("val"));
  });
}

如果另一方面我执行以下操作,则不会调用提交处理程序一点都不没有错误,没有任何线索,没有。我放置一个断点,就像处理程序不存在一样。

If on the other hand I do the following the submit handler does not get called at all. No error, no clue, nothing. I place a break point and it is like the handler is not there.

form.validate({
		submitHandler: function () {
			onFormIsValid($(this));
		}
	});

  form.bind('invalid-form.validate', function() {
    onFormIsInvalid($(this));
  });

推荐答案

您的代码:

form.submit(function () {
    if ($(this).valid())
      onFormIsValid($(this));
    else
      onFormIsInvalid($(this));
});

这似乎是一种非常迂回的方式而且是错误的方法。该插件会自动捕获点击并阻止提交,因此您的自定义 submit()处理程序可能会发生冲突。

This seems like a very roundabout way and is the wrong approach. The plugin automatically captures the click and blocks the submit, so your custom submit() handler will likely conflict.

你可以使用内置的 submitHandler invalidHandler 回调函数......那你就不用担心了如何调用 .valid(),因为 submitHandler 仅在有效时触发, invalidHandler 仅在无效时触发。

You could be using the built-in submitHandler and invalidHandler callback functions... then you would not need to worry about how to call .valid(), since submitHandler only fires when valid and invalidHandler only fires when invalid.

演示1: http://jsfiddle.net/9obe1b35/1/

如果你正在使用ASP内置的Unobtrusive Validation helper插件,那么 .validate()方法是自动构造的,你不能把这些回调函数放在里面。相反,您可以将自定义 submitHandler invalidHandler 函数放入 jQuery.validator.setDefaults()方法

If you're using the Unobtrusive Validation helper plugin built into ASP, then the .validate() method is constructed automatically and you cannot put these callback functions inside. Instead, you can put your custom submitHandler and invalidHandler functions into the jQuery.validator.setDefaults() method.

演示2(不引人注目): http://jsfiddle.net/9obe1b35/2/

Demo 2 (Unobtrusive): http://jsfiddle.net/9obe1b35/2/

这篇关于对象不支持属性或方法'valid'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆