[英]Javascript error: .split is not a function
我正在嘗試將全名分成名字和姓氏並填充相應的字段。 但是,我一直遇到 JavaScript 錯誤“.split is not a function”
這是我的代碼
function SplitName (executionContext) {
var formContext = executionContext.getFormContext();
var FirstName = formContext.getAttribute('firstname');
var LastName = formContext.getAttribute('lastname');
var FullNameField = FullName.getValue();
if (FullNameField =! null){
var SplitField = FullNameField.split(" ",2);
var FirstNameValue = SplitField[0];
var LastNameValue = SplitField[1];
FirstName.setValue(FirstNameValue);
LastName.setValue(LastNameValue);
}
else {
FirstName.setValue('');
LastName.setValue('');
}
}
任何關於我做錯了什么的幫助/建議將不勝感激
FullNameField =! null
您正在將 FullNameField 分配給“true”。 切換到:
FullNameField != null
在對它運行“split”方法之前,變量“FullNameField”應該是一個字符串。
因此,嘗試使用
FullNameField.toString().split(" ",2);
所以不知何故這使它起作用。
function SplitName (executionContext) {
var formContext = executionContext.getFormContext();
var FirstName = formContext.getAttribute('firstname');
var LastName = formContext.getAttribute('lastname');
var FullNameField = FullName.getValue();
if (Lastname != null){
if (FullNameField != null){
var SplitField = FullNameField.split(" ",2);
var FirstNameValue = SplitField[0];
var LastNameValue = SplitField[1];
FirstName.setValue(FirstNameValue);
LastName.setValue(LastNameValue);
}
else {
FirstName.setValue('');
LastName.setValue('');
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.