Javascript 没有“表单”标签的jQuery验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6282680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery validation without "form" tag
提问by Rocky Singh
According to http://docs.jquery.com/Plugins/Validationthe "form" tag is necessary in order to do validation. In my case I don't have form tag. How can I validate(required field) my textbox on click of "button" type control
根据http://docs.jquery.com/Plugins/Validation,“表单”标签是进行验证所必需的。就我而言,我没有表单标签。如何在单击“按钮”类型控件时验证(必填字段)我的文本框
采纳答案by Eric
Why not just add a form tag? If it's an input, then it should normally be part of a form.
为什么不只添加一个表单标签?如果它是一个输入,那么它通常应该是表单的一部分。
回答by brymck
You could always wrap it with a fake form and validate it.
你总是可以用一个假的形式包装它并验证它。
var $textbox = $("#textbox");
$("<form>").append($textbox).validate();
Note, however, that in most cases this should imply that we're going about something wrongly, and I'd consider a form for every element that's submitted in any form (whether it's through standard GET/POST, AJAX, etc.).
但是请注意,在大多数情况下,这应该意味着我们正在做一些错误的事情,我会考虑为以任何形式提交的每个元素(无论是通过标准 GET/POST、AJAX 等)提交一个表单。
回答by Jan Ko?u?ník
I know, it is quite old question, anyway, I had almost the same problem: I had defined form:
我知道,这是一个很老的问题,无论如何,我遇到了几乎相同的问题:我已经定义了形式:
<form id="some-form-id"></form>
And than in document I had inputs like this:
比在文档中我有这样的输入:
<input type="text" form="some-form-id" />
jQuery validator cannot validate this, because elements weren't within the form, so I made small update: There is method elementswhich load elements to validate and I edit it into version bellow this text. I add loading items which are not inside form, which I load into variable outForm. This items are loaded only if form has attribute id. I test it and it works. I hope that this will help to someone.
jQuery 验证器无法验证这一点,因为元素不在表单中,所以我做了一些小更新:有方法元素可以加载要验证的元素,我将其编辑到此文本下方的版本中。我添加了不在表单内的加载项,我将其加载到变量outForm 中。仅当表单具有属性id时才加载此项目。我测试它并且它有效。我希望这会对某人有所帮助。
elements: function () {
var validator = this,
rulesCache = {};
// select all valid inputs inside the form (no submit or reset buttons)
var inForm = $(this.currentForm)
.find("input, select, textarea")
.not(":submit, :reset, :image, [disabled], [readonly]")
.not(this.settings.ignore)
.filter(function () {
if (!this.name && validator.settings.debug && window.console) {
console.error("%o has no name assigned", this);
}
// select only the first element for each name, and only those with rules specified
if (this.name in rulesCache || !validator.objectLength($(this).rules())) {
return false;
}
rulesCache[this.name] = true;
return true;
});
var formId = $(this.currentForm).attr('id');
if(typeof formId == 'undefined')
return inForm;
var outForm = $("input[form='"+formId+"'], select[form='"+formId+"'], textarea[form='"+formId+"']")
.not(":submit, :reset, :image, [disabled], [readonly]")
.not(this.settings.ignore)
.filter(function () {
if (!this.name && validator.settings.debug && window.console) {
console.error("%o has no name assigned", this);
}
// select only the first element for each name, and only those with rules specified
if (this.name in rulesCache || !validator.objectLength($(this).rules())) {
return false;
}
rulesCache[this.name] = true;
return true;
});
return $.merge(inForm,outForm);
},