茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    CKEditor無法驗(yàn)證的解決方案(js驗(yàn)證+jQuery Validate驗(yàn)證)
    來源:易賢網(wǎng) 閱讀:1119 次 日期:2016-07-02 11:49:00
    溫馨提示:易賢網(wǎng)小編為您整理了“CKEditor無法驗(yàn)證的解決方案(js驗(yàn)證+jQuery Validate驗(yàn)證)”,方便廣大網(wǎng)友查閱!

    這篇文章主要為大家詳細(xì)介紹了CKEditor無法驗(yàn)證的解決方案和jQuery Validate驗(yàn)證框架,感興趣的小伙伴們可以參考一下

    最近項(xiàng)目的前端使用了jQuery,表單的前端驗(yàn)證用的是jQuery Validate,用起來很簡(jiǎn)單方便,一直都很滿意的。

    前段時(shí)間,根據(jù)需求為表單中的 textarea 類型的元素加上了html富文本編輯器,用的是CKEditor,功能強(qiáng)大,定制方便,也很滿意。

    不過用CKEditor增強(qiáng)過的 textarea 元素,這個(gè)字段要求是非空的,在jQuery Validate總是驗(yàn)證不通過,原因就是在 CKEditor 編輯器填寫了內(nèi)容之后,編輯器并不是立即把內(nèi)容更新到原來的 textarea 元素中的,我沒仔細(xì)看源代碼,試過一種情況就是每一次提交不通過,第二次提交就可以通過的,貌似編輯器是在 submit 事件之前把編輯器的內(nèi)容更新到 textarea 中的(這個(gè)是猜測(cè),不知道對(duì)不對(duì),我對(duì)jQuery 和 CKEditor 都不太熟悉,算是拿來就用,有問題就放狗的那種)。

    于是在網(wǎng)上找到了解決問題的代碼,代碼不是我寫的,我只是記錄一下我遇到的問題,代碼非原創(chuàng)。原理就是當(dāng)編輯器更新了內(nèi)容之后,立即把內(nèi)容更新到 textarea 元素。 

    CKEDITOR.instances["page_content"].on("instanceReady", function() 

        { 

                //set keyup event 

                this.document.on("keyup", updateTextArea); 

                 //and paste event 

                this.document.on("paste", updateTextArea); 

        }); 

        function updateTextArea() 

        { 

            CKEDITOR.tools.setTimeout( function() 

                  {  

                    $("#page_content").val(CKEDITOR.instances.page_content.getData()); 

                    $("#page_content").trigger('keyup'); 

                  }, 0);  

        } 

    目前一切使用正常,算是解決了一個(gè)讓我頭痛的問題。

    另一種解決思路:

    CKEditor 編輯器是增強(qiáng)過的 textarea 元素,在填寫了內(nèi)容之后,編輯器并不立即把內(nèi)容更新到原來的 textarea 元素中的,而是等到 submit 事件之前把編輯器的內(nèi)容更新到 textarea 中.

    因此,普通的js驗(yàn)證或是jquery validate驗(yàn)證都獲取不到編輯器的值.)

    1.js驗(yàn)證

    獲取CKEditor 編輯器的值其實(shí)很容易,其值就是CKEDITOR.instances.mckeditor.getData(),實(shí)例代碼如下:

    <script language="javascript" type="text/javascript">   

      function checkForm() 

           { 

             var f=document.form1; 

             var topicHeading=f.tbTopicHeading.value; 

             topicHeading = topicHeading.replace(/^\s+/g,""); 

             topicHeading = topicHeading.replace(/\s+$/g,""); 

                     if (topicHeading =="") 

                      { 

                        alert("請(qǐng)輸入發(fā)表話題的標(biāo)題."); 

                        f.tbTopicHeading.focus(); 

                        return false; 

                      } 

                      if(topicHeading.length>50); 

                      { 

                       alert("話題的主題長(zhǎng)度必須在50字符以內(nèi)."); 

                       f.tbTopicHeading.focus(); 

                       return false; 

                      } 

             var topicContent=CKEDITOR.instances.mckeditor.getData(); 

               

             topicContent = topicContent.replace(/^\s+/g,""); 

             topicContent = topicContent.replace(/\s+$/g,""); 

                     if (topicContent =="") 

                      { 

                        alert("請(qǐng)?zhí)顚懺掝}內(nèi)容."); 

                        f.mckeditor.focus(); 

                        return false; 

                      }  

                      if(topicContent.length>4000) 

                      { 

                       alert("話題內(nèi)容的長(zhǎng)度必須在4000字符以內(nèi)."); 

                       f.mckeditor.focus(); 

                       return false; 

                      }       

           }  

           </script> 

    其中,mckeditor為編輯器的textarea的id和name.

    ASP.NET中也是一樣:

    復(fù)制代碼 代碼如下:

    <asp:TextBox ID="mckeditor" runat="server" TextMode="MultiLine" Width="94%" Height="400px" CssClass="ckeditor"></asp:TextBox> 

    2.jQuery Validate驗(yàn)證

    jquery的驗(yàn)證模式不能直接使用CKEDITOR.instances.mckeditor.getData()這個(gè)值.

    它是使用如下形式來提交驗(yàn)證:

    function InitRules() { 

          opts = { 

             rules: 

             { 

                tbTopicHeading:{ 

                required:true, 

                maxlength:50   

              },           

              mckeditor:{ 

                required:true, 

                maxlength:4000 

              }  

             }, 

             messages: 

             { 

              tbTopicHeading:{ 

              required:"請(qǐng)輸入發(fā)表話題的標(biāo)題.", 

              maxlength:jQuery.format("話題的主題長(zhǎng)度必須在50字符以內(nèi).")  

            }, 

              mckeditor:{ 

              required:"請(qǐng)?zhí)顚懺掝}內(nèi)容.", 

              maxlength:jQuery.format("話題內(nèi)容的長(zhǎng)度必須在4000字符以內(nèi).")  

            } 

             }  

          } 

        } 

    其中mckeditor為控件id,不僅有取值的作用,還有提示信息定位的作用.

    因此,可以在頁面加載時(shí),加入實(shí)例化編輯器代碼,實(shí)現(xiàn)編輯器更新了內(nèi)容之后,立即把內(nèi)容更新到 textarea 元素。

    代碼如下:

    <script type="text/javascript"> 

    //<![CDATA[ 

    CKEDITOR.instances["mckeditor"].on("instanceReady", function()    

        {    

                //set keyup event  

                this.document.on("keyup", updateTextArea);  

                 //and paste event 

                this.document.on("paste", updateTextArea);   

        });    

        function updateTextArea()  

        {    

            CKEDITOR.tools.setTimeout( function() 

                  {    

                    $("#mckeditor").val(CKEDITOR.instances.mckeditor.getData());    

                    $("#mckeditor").trigger('keyup');    

                  }, 0);  

        }   

    //]]> 

                  </script> 

    此段代碼放在編輯器控件之下即可.完整實(shí)例如下:

    <asp:TextBox ID="mckeditor" runat="server" TextMode="MultiLine" Width="98%" Height="400px" CssClass="ckeditor"></asp:TextBox> 

    <script type="text/javascript"> 

    //<![CDATA[ 

    CKEDITOR.replace( '<%=mckeditor.ClientID %>',// mckeditor.ClientID為TextBox mckeditor生成的對(duì)應(yīng)客戶端看到的id 

    skin : 'kama',//設(shè)置皮膚 

    enterMode : Number(2),//設(shè)置enter鍵的輸入1.<p>2為<br/>3為<div> 

    shiftEnterMode : Number(1), // 設(shè)置shiftenter的輸入 

    disableNativeSpellChecker:false,  

    scayt_autoStartup:false, 

    toolbar_Full : [ 

    ['Source','-','Save','NewPage','Preview','-'], 

    ['Cut','Copy','Paste','PasteText','PasteFromWord','-'], 

    ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'], 

    ['NumberedList','BulletedList','-','Outdent','Indent'], 

    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'], 

    ['Link','Unlink','Anchor'], 

    ['Image','Table','HorizontalRule'],['Subscript','Superscript'], 

    '/', 

    ['Bold','Italic','Underline'], 

    ['TextColor','BGColor'], 

    ['Styles','Format','Font','FontSize'] 

    ], 

    //filebrowserBrowseUrl: '<%=ResolveUrl("~/ckfinder/ckfinder.html")%>', //啟用瀏覽功能,正式使用場(chǎng)合可以關(guān)閉,只允許用戶上傳 

    //filebrowserImageBrowseUrl:'<%=ResolveUrl("~/ckfinder/ckfinder.html?Type=Images")%>', 

    //filebrowserImageUploadUrl:'<%=ResolveUrl("~/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images")%>' 如果使用ckfinder 就不要屏蔽 

    //自定義的上傳 

    filebrowserImageUploadUrl:'<%=ResolveUrl("~/fileupload/fileupload.aspx?command=QuickUpload&type=Images")%>'

    }); 

    CKEDITOR.instances["mckeditor"].on("instanceReady", function()  

        { 

                //set keyup event 

                this.document.on("keyup", updateTextArea);  

                 //and paste event 

                this.document.on("paste", updateTextArea);  

        });  

        function updateTextArea() 

        { 

            CKEDITOR.tools.setTimeout( function()  

                  { 

                    $("#mckeditor").val(CKEDITOR.instances.mckeditor.getData());  

                    $("#mckeditor").trigger('keyup');  

                  }, 0);  

        }   

    //]]> 

                  </script>

    以上就是解決CKEditor無法驗(yàn)證的兩種方案,相信大家和小編一樣都有所收獲

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)