the front-end of the recent project uses jquery, and the front-end validation of the form uses jquery validate. it is very simple and convenient to use, and i have always been very satisfied.
some time ago, an html rich text editor was added to the textarea type elements in the form according to the needs. i used ckeditor, which is powerful and easy to customize. i am also very satisfied.
however, for the textarea element enhanced with ckeditor, this field is required to be non-empty, and jquery validate always fails the verification. the reason is that after the ckeditor editor fills in the content, the editor does not immediately update the content to the original one. in the textarea element, i didn’t look at the source code carefully. one situation i tried was that every submission failed, but the second submission passed. it seems that the editor updates the editor’s content into the textarea before the submit event. (this is just a guess, i don’t know if it’s correct. i’m not very familiar with jquery and ckeditor. i just use them as they come and let them go if there are any problems).
so i found the code to solve the problem on the internet. the code was not written by me. i just recorded the problems i encountered. the code is not original. the principle is that when the editor updates the content, it immediately updates the content to the textarea element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
everything is working normally now, which solves a headache problem for me.
another solution:
ckeditor editor is an enhanced textarea element. after filling in the content, the editor does not immediately update the content to the original textarea element. instead, it waits until the submit event to update the editor content to the textarea. .
therefore, ordinary js validation or jquery validate validation cannot obtain the editor value.)
1.js verification
it is actually very easy to get the value of the ckeditor editor. the value is ckeditor.instances.mckeditor.getdata(). the example code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
among them, mckeditor is the id and name of the editor's textarea.
the same is true in asp.net:
2.jquery validate
jquery’s verification mode cannot directly use the value of ckeditor.instances.mckeditor.getdata().
it is submitted for verification using the following form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
among them, mckeditor is the control id, which not only has the function of obtaining value, but also has the function of positioning prompt information.
therefore, you can add instantiation editor code when the page is loaded, so that after the editor updates the content, it will immediately update the content to the textarea element.
the code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
this code can be placed under the editor control. the complete example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
the above are two solutions to the problem that ckeditor cannot verify. i believe that everyone will gain something like the editor. thank you for reading.