将 jQuery Validate 插件与 TinyMCE 编辑器结合使用

发布于 2024-12-26 03:05:19 字数 2925 浏览 1 评论 0原文

我正在使用 jQuery 的 Validate 插件实现客户端验证,但缺少使用 TinyMCE 编辑器组件的表单字段。这会在我的表单字段的包含标记下呈现一个极其复杂的控制树,其中包括用于实际编辑区域的 iframe 和一个 textarea 元素。我无法直接访问 textarea,因此我在调用 .validate() 之前添加了 'required' 属性,如下所示:

jQuery(function() {
    jQuery("#wpsm_body").addClass("required");
    jQuery("#wpsm-send-mail")
            .validate(
                    {
                        errorContainer : "#wpsm-top-error-container, #wpsm-bottom-error-container",
                        errorLabelContainer : "#wpsm-top-error-container ul",
                        wrapper : "li",
                        messages : {
                            wpsm_from : "The message has no 'From' address. The administrator can set a default for this on the SuperMail 'Settings' page.",
                            wpsm_subject : "The message has no subject.",
                            wpsm_body : "The message has no body.",
                            wpsm_text : "The message has no body."
                        }
                    });
});

但是,当我提交时,我没有收到客户端验证错误即使 wpsm_body 为空。

作为背景,这是在 WordPress 插件内,TinyMCE 由 wp_editor 函数呈现,尽管我怀疑这会产生太大的差异。

已添加:虽然我认为这不会有太大帮助,但这里是呈现给浏览器的 HTML。随后 TinyMCE 生成的 HTML 长达 400 行,并且不相关。

<form id="wpsm-send-mail" action="theform.php" method="POST" enctype="multipart/form-data">
<div id="wp-wpsm_body-wrap" class="wp-editor-wrap tmce-active">
    <link rel='stylesheet' id='editor-buttons-css' href='http://localhost/wordpress/wp-includes/css/editor-buttons.dev.css?ver=20111114'
        type='text/css' media='all' />
    <div id="wp-wpsm_body-editor-tools" class="wp-editor-tools">
        <a id="wpsm_body-html" class="hide-if-no-js wp-switch-editor switch-html" onclick="switchEditors.switchto(this);">
            HTML</a> <a id="wpsm_body-tmce" class="hide-if-no-js wp-switch-editor switch-tmce"
                onclick="switchEditors.switchto(this);">Visual</a>
        <div id="wp-wpsm_body-media-buttons" class="hide-if-no-js wp-media-buttons">
            <a href="http://localhost/wordpress/wp-admin/media-upload.php?post_id=0&#038;TB_iframe=1"
                class="thickbox add_media" id="wpsm_body-add_media" title="Add Media" onclick="return false;">
                Upload/Insert
                <img src="http://localhost/wordpress/wp-admin/images/media-button.png?ver=20111005"
                    width="15" height="15" /></a></div>
    </div>
    <div id="wp-wpsm_body-editor-container" class="wp-editor-container">
        <textarea class="wp-editor-area" rows="20" cols="40" name="wpsm_body" id="wpsm_body"></textarea></div>
</div>
</form>

这里唯一相关的项目是 formtextarea 及其 id 属性。

I am implementing client validation using the Validate plugin for jQuery, and coming short with a form field that uses the TinyMCE editor component. This renders an incredibly complex control tree under the containing tag for my form field, including an iframe for the actual editing area, and a textarea element. I cannot access the textarea directly, so I add the 'required' attribute before calling .validate(), like below:

jQuery(function() {
    jQuery("#wpsm_body").addClass("required");
    jQuery("#wpsm-send-mail")
            .validate(
                    {
                        errorContainer : "#wpsm-top-error-container, #wpsm-bottom-error-container",
                        errorLabelContainer : "#wpsm-top-error-container ul",
                        wrapper : "li",
                        messages : {
                            wpsm_from : "The message has no 'From' address. The administrator can set a default for this on the SuperMail 'Settings' page.",
                            wpsm_subject : "The message has no subject.",
                            wpsm_body : "The message has no body.",
                            wpsm_text : "The message has no body."
                        }
                    });
});

However, I get no client validation error when I submit the form even with an empty wpsm_body.

For background, this is inside a WordPress plugin, and TinyMCE is rendered by the wp_editor function, although I doubt this makes too much difference.

Added: While I don't believe this will help much at all, here is the HTML rendered to the browser. The HTML generated by TinyMCE subsequent to this is 400 lines long, and not relevant.

<form id="wpsm-send-mail" action="theform.php" method="POST" enctype="multipart/form-data">
<div id="wp-wpsm_body-wrap" class="wp-editor-wrap tmce-active">
    <link rel='stylesheet' id='editor-buttons-css' href='http://localhost/wordpress/wp-includes/css/editor-buttons.dev.css?ver=20111114'
        type='text/css' media='all' />
    <div id="wp-wpsm_body-editor-tools" class="wp-editor-tools">
        <a id="wpsm_body-html" class="hide-if-no-js wp-switch-editor switch-html" onclick="switchEditors.switchto(this);">
            HTML</a> <a id="wpsm_body-tmce" class="hide-if-no-js wp-switch-editor switch-tmce"
                onclick="switchEditors.switchto(this);">Visual</a>
        <div id="wp-wpsm_body-media-buttons" class="hide-if-no-js wp-media-buttons">
            <a href="http://localhost/wordpress/wp-admin/media-upload.php?post_id=0&TB_iframe=1"
                class="thickbox add_media" id="wpsm_body-add_media" title="Add Media" onclick="return false;">
                Upload/Insert
                <img src="http://localhost/wordpress/wp-admin/images/media-button.png?ver=20111005"
                    width="15" height="15" /></a></div>
    </div>
    <div id="wp-wpsm_body-editor-container" class="wp-editor-container">
        <textarea class="wp-editor-area" rows="20" cols="40" name="wpsm_body" id="wpsm_body"></textarea></div>
</div>
</form>

The only relevant items here are the form and textarea, and their id attributes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2025-01-02 03:05:19

假设您已经包含了tinyMCE的js文件,并且您正在使用tinyMCE在客户端进行验证,

以下是验证tinyMCE编辑器的代码

<script>  
    $(document).ready(function () {  

        var $btn = $("#<%=btnSubmit.ClientID %>");  
        var $txtEditor = $(".txtEditor");  

        $btn.click(function () {  
            if (tinyMCE.get($txtEditor.attr("id")).getContent() == "") {  
                alert("hi");  
            }  
            else {  
                alert("bye");  
            }  
            return false;  
        })  

    });  

</script>  


<div>  
<asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine" CssClass="txtEditor212"></asp:TextBox>  
            <asp:TextBox id="txtEditor" runat="server" TextMode="MultiLine" CssClass="txtEditor"></asp:TextBox>  
        </div>  
        <div>  
        <asp:Button id="btnSubmit" runat="server" Text="Save" onclick="btnSubmit_Click" />  

        </div>  
</asp:Content>
</div>

Assuming You have already included the js file for tinyMCE and you are doing the validation on client side using tinyMCE

Following is the code to validate the tinyMCE editor

<script>  
    $(document).ready(function () {  

        var $btn = $("#<%=btnSubmit.ClientID %>");  
        var $txtEditor = $(".txtEditor");  

        $btn.click(function () {  
            if (tinyMCE.get($txtEditor.attr("id")).getContent() == "") {  
                alert("hi");  
            }  
            else {  
                alert("bye");  
            }  
            return false;  
        })  

    });  

</script>  


<div>  
<asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine" CssClass="txtEditor212"></asp:TextBox>  
            <asp:TextBox id="txtEditor" runat="server" TextMode="MultiLine" CssClass="txtEditor"></asp:TextBox>  
        </div>  
        <div>  
        <asp:Button id="btnSubmit" runat="server" Text="Save" onclick="btnSubmit_Click" />  

        </div>  
</asp:Content>
</div>
乖乖兔^ω^ 2025-01-02 03:05:19

我不知道TinyMCE在Wordpress中具体是如何使用的。

但我以前使用过 TinyMCE,当您在富文本编辑器中进行编辑时,textarea 不会更新(它会在表单提交时更新)。当我需要(ajax)时,我使用以下代码来更新文本区域:

form.find('textarea.rich').each(function() {
    var textarea = $(this);
    var taeditor = tinyMCE.get(textarea.attr('id'));
    if (taeditor) 
        textarea.val(taeditor.getContent());
});

希望这有帮助。

I don't know exactly how TinyMCE is used in Wordpress.

But I've used TinyMCE before, and the textarea doesn't get updated as you make edits in the rich text editor (it does get updated on form submit). I used the following code to update the textarea when I needed to (ajax):

form.find('textarea.rich').each(function() {
    var textarea = $(this);
    var taeditor = tinyMCE.get(textarea.attr('id'));
    if (taeditor) 
        textarea.val(taeditor.getContent());
});

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文