简体   繁体   English

Javascript 错误:.split 不是函数

[英]Javascript error: .split is not a function

I'm trying to separate a full name into first and last name and populate the corresponding fields.我正在尝试将全名分成名字和姓氏并填充相应的字段。 However, I keep running into a JavaScript error ".split is not a function"但是,我一直遇到 JavaScript 错误“.split is not a function”

This is my code这是我的代码

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('');
    }​​​
}​​​

Any help/suggestion on what am I doing wrong would be greatly appreciated任何关于我做错了什么的帮助/建议将不胜感激

FullNameField =! null

You're assigning FullNameField to "true" with this.您正在将 FullNameField 分配给“true”。 Switch it to:切换到:

FullNameField != null

the variable "FullNameField" should be a string before you run "split" method on it.在对它运行“split”方法之前,变量“FullNameField”应该是一个字符串。

Hence, try using因此,尝试使用

FullNameField.toString().split(" ",2);

So somehow this made it work.所以不知何故这使它起作用。

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