与 ckeditor 集成的文本区域的 jquery 验证
我有一个
<td><textarea id="event-body" name="body">
<p class="error"></p>
与 CKEDITOR 集成的
CKEDITOR.replace("event-body")
text-area
和 jquery validate 插件。代码是这样的
$('#event').validate({
rules:{
name:{
required: true
},
},
messages:{
body:{
required: "event body is required"
}
},
errorPlacement: function(error, element){
$(element).each(function (){
$(this).parent('td').find('p.error').html(error);
})
});
代码工作得很好,但是当我输入我的 textarea
元素时,我仍然收到错误消息,直到我单击它两次。即我必须提交页面两次,这样即使 textarea
不为空也不会出现错误消息。
有没有办法顺利验证它(无需单击两次)。
I have a text-area
<td><textarea id="event-body" name="body">
<p class="error"></p>
That is integrated with CKEDITOR
CKEDITOR.replace("event-body")
And jquery validate plugin. And the code is like this
$('#event').validate({
rules:{
name:{
required: true
},
},
messages:{
body:{
required: "event body is required"
}
},
errorPlacement: function(error, element){
$(element).each(function (){
$(this).parent('td').find('p.error').html(error);
})
});
The code works just fine but when I type into my textarea
element, I still get the error message until I click it twice. i.e. I have to submit my page twice so that I don't error message even if textarea
is not empty.
Isn't there a way to validate it smoothly(without having to click it twice).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
看看这里
基本上你需要
在运行验证之前调用。
只需将
editor1
替换为您的文本区域的名称即可。然后致电
编辑
Take a look here
Basically you need to call
before running validation.
Just replace
editor1
with the name of your textarea.Then call
EDIT
将其放入提交按钮 mousedown() 事件中:
Put this in submit button mousedown() event:
jQuery 验证器仅验证可见的输入字段,但 CKEDITOR 使文本区域隐藏以防止其被验证。
jQuery validator only validate input fields that are visible but CKEDITOR makes the textarea hidden preventing it from being validated.
我结合了你们的建议,制作了这个没有问题的小作弊程序。
我的代码:
在我的 .css 文件上,我强迫我的代码如下:
我的 .js 验证是正常格式:
I've combined your sugestions and make this little cheat that works with no problem.
My code:
On my .css file, I forced my to be like this:
And my .js validation is the normal format:
在验证表单的 JavaScript 中使用它。它将更新您的 ckeditor 并将 ckeditor 的值分配给您将其集成到的文本区域。
它应该在提交表单之前运行:
Use this in your javascript where you are validating your form. It will update your ckeditor and will assign ckeditor's value to your textarea to which you integrate it.
It should run before form is submitted:
如果您像这样附加它
,那么您将需要使用类似的东西,
在这种情况下我使用这个示例函数来提交我的表单
If you are attaching it like
than you will need to use something like this
I use this example function to submit my form in this case
我更愿意更新模糊事件的每个实例,因此您不需要更改提交功能上的任何内容! :)
I would prefer to update each instance on blur event, so you don't need to change anything on your submit function! :)
jQuery .validate() 函数提供“onsubmit”选项来在验证之前执行自定义操作。
默认情况下它设置为 true。
我已经在我的项目中使用了它并且效果非常好。
这是一种更干净、更容易理解的方法。
请参阅http://jqueryvalidation.org/validate#onsubmit
jQuery .validate() function provides "onsubmit" option to perform custom action before validation.
By default it is set to true.
I have used it in my project and it works very well.
This is a cleaner and much easier to understand approach.
Refer http://jqueryvalidation.org/validate#onsubmit
如果你使用 SPRY,这对我有用......
IF your using SPRY, This is what worked for me....