繁体   English   中英

Javascript 错误:.split 不是函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM