我有一个验证例程,我保存在我的SSJS脚本库中。当我点击保存按钮时会调用它。目的(在这里的几个帖子中看到)是执行表单验证,创建错误消息,然后onClientLoad事件将显示消息并将焦点返回到它找到的第一个问题字段。
以下是代码段;来自SSJS脚本库:
contentViewController

function DBValidate(FormName:String) {
var efn:string = null; // error Field Name
var errorMessages:String = null;
// here is where we group validation by the calling form.
switch (FormName) {
case "FieldDef":
if ( isEmpty(currentDocument.getItemValue("FormName")[0]) ) setError("FormName","Form name cannot be blank");
if ( isEmpty(currentDocument.getItemValue("EnglishFormName")[0]) ) setError("EnglishFormName","English form name cannot be blank");
break
default:
}
// We built the error messages (All will be displayed in a promptbox in the onClientLoad event
// of the form) The form MUST have an ErrorMessage field because we embed carriage returns
// in the message and viewScope.get doesn't like those too much..... We can however pass
// the error field(for focus if I ever get that working.....)in a viewScope variable.
getComponent("ErrorMessage").setValue(errorMessages);
viewScope.put("errorField",efn);
if (errorMessages == null ) {
return true;
} else {
return false;
}
}
function setError(fName:string,emsg:string){
// after failing a field validation in DBValidate we
// make note of the field name and build the error message that
// we're going to display in the onClientLoad event.
// Make note of the first field ID to fail so we can
// set focus to it.
if (efn==null) efn=getClientId(fName);
if (errorMessages == null) {
errorMessages = String.fromCharCode(10) + emsg;
} else {
errorMessages += String.fromCharCode(10) + emsg;
}
return
}

ef.focus()实际上有效。我可以看到光标闪烁到第一个有错误的字段,但随后就消失了。
我不知道造成这种情况的原因。我尝试跟随调试器,但在走出onClientLoad事件后,我开始徘徊在非常密集的代码中(显示UI的上部显示为灰色,所以我真的可以告诉我何时应用焦点然后删除我没有定义任何其他事件。
答案 0 :(得分:2)
请在return false
行之后添加ef.focus()
。这将停止执行任何服务器端代码,使其立即运行。
答案 1 :(得分:0)
I have had similar issues where some "under the hood Dojo magic" is going on - and therefore moves the focus.
The way I have gotten around it is a little "hack" where I use a client side timeout function to delay setting the focus 100-200 ms.
Not ideal, but I gave up trying to make it work directly ;-)
/John