nicEdit textarea名称来做检查

时间:2014-06-19 04:32:40

标签: javascript textarea nicedit

我想检查我的textarea,如果用户没有填写消息,它会提示一条警告消息。然而,我的textarea似乎被nicEdit textarea取代了。

HTML部分

<head>
<script type="text/javascript" src="/ecover/common/editor/editor.js"></script> 
<script type="text/javascript">
bkLib.onDomLoaded(function() {new nicEditor({fullPanel : true}).panelInstance('MESSAGE');});
function validate()
{ 
 if(document.mainfrm.MESSAGE.value=="")
   { 
     alert("Please enter the value Message");
     document.mainfrm.MESSAGE.focus();
     return false;
   }
    return true;
}
</script>
</head>
<body> 
  <form name="mainfrm" method="post" action="addMessageRep.jsp" onSubmit="return validate();">   
    <textarea id="MESSAGE" name="MESSAGE" cols="80" rows="20" ></textarea>
  </form> 
</body>

nicEdit.js部分受影响区域

var isTextarea = (e.nodeName.toLowerCase() == "textarea");
    if(isTextarea || this.options.hasPanel) {

        var ie7s = (bkLib.isMSIE && !((typeof document.body.style.maxHeight != "undefined") && document.compatMode == "CSS1Compat"))
        var s = {width: newX+'px', border : '1px solid #ccc', borderTop : 0, overflowY : 'auto', overflowX: 'hidden' };
        s[(ie7s) ? 'height' : 'maxHeight'] = (this.ne.options.maxHeight) ? this.ne.options.maxHeight+'px' : null;
        this.editorContain = new bkElement('DIV').setStyle(s).appendBefore(e);
        var editorElm = new bkElement('DIV').setStyle({width : (newX-8)+'px', margin: '4px', minHeight : newY+'px'}).addClass('main').appendTo(this.editorContain);

        e.setStyle({display : 'none'});

        editorElm.innerHTML = e.innerHTML;      
        if(isTextarea) {
            editorElm.setContent(e.value);
            this.copyElm = e;
            var f = e.parentTag('FORM');
            if(f) { bkLib.addEvent( f, 'submit', this.saveContent.closure(this)); }
        }
        editorElm.setStyle((ie7s) ? {height : newY+'px'} : {overflow: 'hidden'});
        this.elm = editorElm;   
    }

测试步骤:

  1. 首次尝试在textarea中填写“test”值并点击“保存”按钮时,会提示“请输入值信息”
  2. 关闭消息框并再次单击“保存”按钮后,保存该值。据说如果我在textarea中填写值,它应该保存值而不是提示消息。

1 个答案:

答案 0 :(得分:0)

获取niceEditor textarea的值,尝试:

var nicInstance = nicEditors.findEditor('MESSAGE');
var messageContent = nicInstance.getContent();
//then check for messageContent

<强>更新::

bkLib.onDomLoaded(function() {new nicEditor({fullPanel : true}).panelInstance('MESSAGE');});
function validate()
{ 
    var nicInstance = nicEditors.findEditor('MESSAGE');
    var messageContent = nicInstance.getContent();
    //since nicEditor sets default value of textarea as <br>
    //we are checking for it
    if(messageContent=="<br>") { 
     alert("Please enter the value Message");
     document.mainfrm.MESSAGE.focus();
     return false;
   }
   else {
        alert("valid");
   }   
    return true;
}

演示:: jsFiddle