2012-01-11 81 views
2

我正在使用jQuery的Validate插件实现客户端验证,并且简短地介绍了使用TinyMCE编辑器组件的表单字段。这在我的表单字段的包含标记下呈现了令人难以置信的复杂控制树,其中包括用于实际编辑区域的iframe以及textarea元素。我不能直接访问的textarea,所以我打电话.validate()前添加“必要”的属性,如下图所示:在TinyMCE编辑器中使用jQuery Validate插件

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属性。

+0

一些HTML将是很好的代码 – ori 2012-01-11 08:43:00

+0

@ori,看我的编辑。我已经添加了可以添加的HTML。 – ProfK 2012-01-11 09:40:22

回答

2

假设您已经包含了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> 
0

我不确切知道在Wordpress中如何使用TinyMCE。

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

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

希望这有助于。

+0

谢谢,但我的问题不是textarea没有得到更新。即使没有更新,即保持空白,我也没有验证错误。 – ProfK 2012-01-12 15:19:13

+0

你是对的,我的回答可能会帮助下一个问题...除了你的代码看起来应该可以工作,所以一些好的旧调试的顺序:当wp创建编辑器时textarea保持空白,wp是否重复textarea的ID?等等 – ori 2012-01-12 15:46:14