jQuery Validation插件不会在click事件处理程序中validation

$("#tournamentDesignTabs").tabs(); $("#tournamentDesign").validate(); $("#createTournament").click(function(){ if($("#tournamentDesign").validate()){ $.post( "createTournament.php", {tournamentName: $("#tournamentName").val(), district: $("#district").val(), region: $("#region").val(), location: $("#location").val(), date: $("#date").val(), time: $("#time").val(), logo: $("#logo").val()}, function(responseText){ $("#result").html(responseText); }, "html" ) } else { alert("oh no"); } } ); 

所以上面是我用来validation某些表单信息然后将其输入数据库的脚本。 我遇到的问题是,当附加到span的click事件处理程序时,validate函数似乎没有做任何事情。 我也注意到onfocusout似乎也没有正常运行。 关于我做错了什么的任何建议或想法。 我知道我已正确包含插件,因为validationfunction在我提交表单时仍然有效。 我更喜欢ajax。 谢谢!

编辑:正如下面的一个答案,他们认为有html会有所帮助所以这里是页面。 最后一点代码和内容。 我知道它很脏而且很乱,但我正在学习并很快就会清理它。 无论如何它在这里是:

    Tournament Designer          
Tournament Designer

Enter the details concerning the tournament name, location, date, etc. here.
(* denotes a required field)

Tournament Name:*
District: Provo SLC *
Region: West-USA *
Location:*
Date:*
Time:*
Logo:*AddRemove

Set the pricing structure here. You can add as many structures as you like. To remove a structure select it in the textbox below including the comma and delete it.

# of Divisions: Price:Add
Pricing Structures:

Add Promo Codes Here. A valid Promo Code will contain any character between AZ and 0-9. When creating a Promo Code you can set it to discount by a percentage or fixed dollar amount. Percentage is selected by default. To remove a promo code follow the same steps as above.

Promo Codes:
Code: Discount(Percent[%] or Dollars[$]): Add

Skill & Weight Divisions

Enter Top Level Divisions Here:+

Mat Design

Add Mat Area With 4 mats. Add
I'm here! $("#tournamentDesignTabs").tabs(); //$("#tournamentDesign").validate(); $("#createTournament").click(function(){ if($("#tournamentDesign").validate()){ $.post( "createTournament.php", {tournamentName: $("#tournamentName").val(), district: $("#district").val(), region: $("#region").val(), location: $("#location").val(), date: $("#date").val(), time: $("#time").val(), logo: $("#logo").val()}, function(responseText){ $("#result").html(responseText); }, "html" ) } else { alert("oh no"); } } );

.validate()初始化form上的Validation插件。

.valid()返回truefalse具体取决于您的表单目前是否有效。

所以你在if语句中使用.valid() ,而不是.valid()来测试表单是否有效……

 $("#tournamentDesign").validate(); $("#createTournament").click(function(){ if($("#tournamentDesign").valid()){ ... 

有关详细信息,请参阅docs.jquery.com/Plugins/Validation/valid 。