我想检查我的文本区域,如果用户不填写消息,它将提示出一个警告消息。然而,我的文本区域似乎被一个nicEdit文本区域所取代。
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;
}
测试步骤:
- 第一次尝试在文本区填写"test"值,点击保存按钮,会提示"请输入值信息"
- 关闭消息框后,再次点击保存按钮,保存该值。如果我在文本框中填写值,它应该保存值而不是提示信息。
获取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