Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么JQuery valid()方法总是返回True?_Javascript_Jquery_Jquery Validate - Fatal编程技术网

Javascript 为什么JQuery valid()方法总是返回True?

Javascript 为什么JQuery valid()方法总是返回True?,javascript,jquery,jquery-validate,Javascript,Jquery,Jquery Validate,我有一个简单的HTML表单: <form id="frmNewCategory"> <span>New Category Name:</span> <input type="text" id="txtNewCategoryName"> <label>Amount:</label> <input type="text" id="txtNewCategoryAmount"> &

我有一个简单的HTML表单:

<form id="frmNewCategory">
    <span>New Category Name:</span>
    <input type="text" id="txtNewCategoryName">
    <label>Amount:</label>
    <input type="text" id="txtNewCategoryAmount">
    <br>
    <input type="submit" value="Create" class="importantButton button" id="btnNewCategory">
    <input type="button" value="Cancel" class="button" id="btnCancelNewCategory">
</form>
上述方法旨在验证frmNewCategory。问题是,即使表单有无效值或根本没有值,.valid()方法仍然返回True

有什么想法吗?我做错了什么?

而不是:

<input type="text" id="txtNewCategoryName">
<input type="text" id="txtNewCategoryAmount">

使用:


规则的形式是输入“名称”而不是“ID”:


这种模式以前对我很有效:

$(".required").filter(function () { return $(this).val() == ""; }).addClass("invalidInput");

if (this.formWrp.valid() && this.formWrp.find(".invalidInput").length == 0) 
     return true;

执行此操作后,验证似乎已激活。

谢谢!这是一个需要检测的棘手问题,因为尽管valid()方法将返回true,但调用.validate().element(“#selector”)方法将返回false。在阅读了两个教程之后,我自己已经浪费了6个小时的时间。我将花时间写我自己的教程,用大词强调这个重要的问题。
<input type="text" name="txtNewCategoryName">
<input type="text" name="txtNewCategoryAmount">
      <input type="text" name="txtNewCategoryName" />
      <input type="text" name="txtNewCategoryAmount" />
$(".required").filter(function () { return $(this).val() == ""; }).addClass("invalidInput");

if (this.formWrp.valid() && this.formWrp.find(".invalidInput").length == 0) 
     return true;