2012-04-25 109 views
3

我有一個包含多個TinyMCE編輯器的表單。一些編輯是先進的,有些是簡單的編輯。我在客戶端使用了jquery validation plugin進行驗證。我一直在通過添加以下代碼來驗證單個TinyMCE編輯器。驗證多個TinyMCE編輯器

$('#submit').click(function() { 

    var content = tinyMCE.activeEditor.getContent(); // get the content 

    $('#description').val(content); // put it in the textarea 

}); 

但現在我應該驗證所有的編輯器,任何想法?

回答

10

嘗試

$('#submit').click(function() { 

    for (i=0; i < tinymce.editors.length; i++){ 
    var content = tinymce.editors[i].getContent(); // get the content 

    $('#description').val(content); // put it in the textarea 
    } 
}); 

或更容易

$('#submit').click(function() { 
    tinymce.triggerSave(); 
}); 
+0

這是我在找什麼..但在此代碼也將每個編輯器的內容放入'description'中,我們可以以某種方式獲得相應的textarea的id,以便我們將編輯器中的內容放到相應的textarea中。 – uttam 2012-04-25 10:01:33

+0

使用tinymce.editors [i] .id – Thariama 2012-04-25 10:13:21

+1

可以得到編輯器的id(它們等於textarea ID),以便將編輯器的內容放到相應的textarea中,也可以使用tinymce.triggerSave()一次爲所有的tinymce編輯器執行此操作! – Thariama 2012-04-25 10:14:40

0

相反,你可以將TinyMCE的isDirty()方法

編號:Here

使用

tinyMCE.activeEditor.isDirty()

希望這有助於

0

嘗試這樣

Java腳本代碼

jQuery(document).ready(function(){ 
      // binds form submission and fields to the validation engine 
      jQuery("#formID").validationEngine(); 
     }); 

HTML代碼

首先TinyMCE的

<textarea rows="" cols="" id="first" class="validate[required] text-input tinymce"></textarea> 

其次首先TinyMCE的

<textarea rows="" cols="" id="second" class="validate[required] text-input tinymce"></textarea> 

我也用過這樣的,它的正常工作我

+1

它驗證編輯,但沒有正確驗證作爲在其他形式的元素。我的問題是提交按鈕需要點擊兩次以獲得響應......它必須在第一次點擊時給予響應,如其他表單元素驗證... – uttam 2012-04-25 07:16:58

0

F你有HTML編輯器TinyMCE的要求的驗證工作不正常,你可以用這個代碼來解決你的問題, 在你的應用程序中安裝tinymce 如果你有多個Htmleditor,你可以這樣做 我知道這是正確的解決方案,但你可以做到這一點,並解決這個問題o

在模型中給出tinymce的路徑。CSHTML頁確定

[Required(ErrorMessage = "Please enter About Company")] 
    [Display(Name = "About Company : ")] 
    [UIHint("tinymce_jquery_full"), AllowHtml] 
    public string txtAboutCompany { get; set; } 

    [Required(ErrorMessage = "Please enter About Company")] 
    [Display(Name = "About Company : ")] 
    [UIHint("tinymce_jquery_full"), AllowHtml] 
    public string txtAboutCompany { get; set; } 

現在,在您的視圖中添加一個跨度這樣

<div class="divclass"> 
    @Html.LabelFor(model => model.txtAboutCompany, new { @class = "required" }) 
    @Html.EditorFor(model => model.txtAboutCompany) 
    <span class="field-validation-error" id="AC" style="margin:9px 0 0 157px;"></span> 
</div> 
<div class="divclass"> 
    @Html.LabelFor(model => model.txtAboutCompany, new { @class = "required" }) 
    @Html.EditorFor(model => model.txtAboutCompany) 
    <span class="field-validation-error" id="AC" style="margin:9px 0 0 157px;"></span> 
</div> 

創建的jQuery的提交按鈕點擊事件

$("#BusinessProfile").click(function() { 
    var aboutC = $("#txtAboutCompany").val() 
    var pinfo = $("#txtProductinfo").val(); 
    if (aboutC == "" && pinfo == "") { 
     $("#AC").append("").val("").html("Please enter about company") 
     $("#PI").append("").val("").html("Please enter product information") 
     $("#bpform").valid(); 
     return false; 
    } else if (aboutC == "") { 
     $("#PI").append("").val("").html("") 
     $("#AC").append("").val("").html("Please enter about company") 
     $("#txtAboutCompany").focus(); 
     $("#bpform").valid(); 
     return false; 
    } else if (pinfo == "") { 
     $("#AC").append("").val("").html("") 
     $("#PI").append("").val("").html("Please enter product information") 
     $("#txtProductinfo").focus(); 
     $("#bpform").valid(); 
     return false; 
    } 
    else { 
     $("#AC").append("").val("").html(""); 
     $("#PI").append("").val("").html(""); 
     //return true; 
     $("#bpform").validate(); 
    } 
}); 

我希望你的問題可能是解決

1

更通用的方法是將編輯器內容保存回textarea,然後讓jquer Ÿ驗證做它在textarea的本身

tinymce.init({ 
    setup: function (editor) { 
     editor.on('init change', function() { 
      editor.save(); 
     }); 
    } 
}); 

魔法由於TinyMCE的textarea的是隱藏的,你也必須更新您的jQuery驗證碼這樣的事情

$('#myform').validate({ 
    ignore: ':hidden:not(textarea)' 
});