我知道这很简单(无论如何根据插件页面)
但我试图使用插件没有成功,这是我的形式:
<form id="mongoForm"> <label for="skip">skip:</label> <input type="text" id="skip" style="width: 70px"> <p /> limit: <span id="limitLable" style="border: 0">1</span> <div id="limit" style="width: 250px"></div> <p /> <label for="collection">collection: </label> <input type="text" id="collection"> <p /> <label for="sort">sort: </label> <input type="text" id="sort"> <p /> <label for="type">type: </label><select id="type"> <option value="find" selected="selected">find</option> <option value="count">count</option> </select> <p /> <label for="query">query: </label> <textarea rows="10" cols="80" id="query"></textarea> <input type="submit" value="execute" id="execute"> </form>
和我的jquery脚本:
$(document).ready(function() { $.validator.addMethod("validJson",function(value,element) { try { var val = $.parseJSON(value); return true; } catch (e) { return false; } },"Invalid JSON string"); $("#mongoForm").validate({ rules : { collection : { required : true },query : { validJson : true } },submitHandler : function(form) { queryServer(); } });
但是,验证无效.
如果我在输入中添加class =“required”,它适用于自定义验证,但不适用于我的自定义validJson方法.但这并不能解决我的问题,因为不需要某些字段我只需要验证可选值
有什么想法吗?
解决方法
您需要名称属性,如
the general guidelines:中所述
The name attribute is required for input elements,the validation
plugin doesn’t work without it. Usually name and id attributes should
have the same value.
所以标记应该是
<label for="collection">collection:</label> <input type="text" id="collection" name="collection">
等等