我有一些功能来验证这样的输入字段:
function validateName() {
var inputName = document.getElementById('name');
var pesanError ='';
if (inputName.value ===""){
pesanError = 'Name can't be empty'
tambahError('name',pesanError)
}else if(inputName.value !="" && inputName.value.length < 3){
remove('name');
pesanError = 'Name is too short'
tambahError('name',pesanError)
}else{
remove('name')
}
}
我使用像这样的添加事件:
var inputName = document.getElementById('name');
inputName.addEventListener("blur", validateName, true);
它工作得很好,当它清空时显示&#34;名字不能为空&#34;当有值但长度小于3时,它将显示&#34;名称太短&#34;,但问题是它有值并显示&#34;名称太短&#34;然后该字段再没有价值它仍然显示&#34;名称太短&#34;。所以我的问题是如何才能使我的功能如此显示&#34;名称不能为空&#34;再次当价值再次变为无价值时。
这里删除函数和tambahError函数:
function remove(id) {
var element = id + "Error";
var inputName = document.getElementById(element);
if (inputName !== undefined){
inputName.remove();
}
}
function tambahError(kolom,pesan) {
if (document.getElementById(kolom+"Error")== undefined){
var inputName = document.getElementById(kolom);
var h = document.createElement("span");
h.id = kolom+"Error";
var t = document.createTextNode(pesan);
h.appendChild(t);
h.className='error';
insertAfter(h, inputName);
}
}
答案 0 :(得分:0)
试试这个
pesanError = 'Name can't be empty'
将字符串更改为
pesanError = 'Name can\'t be empty'
这应该可以解决错误
答案 1 :(得分:0)
我们不知道您的tambahError和pesanError以及删除操作。
试试这个。
func main() {
// Get the data from the DB
recipientsList := dbRetrieval()
fmt.Println("About to call the method")
// Invoke the Loop for all the recipients
for indx := range recipientsList {
time.Sleep(time.Second * 10)
fmt.Println("The 3 second wait :", indx)
tokenValueToBeUsed := requestRefreshToken(recipientsList[indx])
if len(tokenValueToBeUsed) == 0 {
err_uid := updateIsDeleted(recipientsList[indx].UserId)
if err_uid {
fmt.Println("Zero refresh token , so updated the DELETE")
} else {
fmt.Println("Zero refresh token But couldnt update the DELETE ")
}
} else {
secret, err := ioutil.ReadFile("client_secret.json")
if err != nil {
log.Printf("Error: %v", err)
} else {
conf, err := google.ConfigFromJSON(secret, gmail.GmailSendScope)
if err != nil {
log.Printf("Error: %v", err)
} else {
var tok oauth2.Token
tok.AccessToken = tokenValueToBeUsed
token := &tok
client := conf.Client(oauth2.NoContext, token)
gmailService, err := gmail.New(client)
if err != nil {
log.Printf("Error: %v", err)
} else {
var message gmail.Message
// For HTML
header := make(map[string]string)
header["From"] = recipientsList[indx].From_Mail
header["To"] = recipientsList[indx].To_Mail
header["Subject"] = recipientsList[indx].Title + "\r\n\r\n" + recipientsList[indx].Body + "\r\n\r\n" + recipientsList[indx].Signature + "\r\n\r\n" + recipientsList[indx].Pixel
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
var msg string
for k, v := range header {
msg += fmt.Sprintf("%s: %s\r\n", k, v)
}
message.Raw = base64.URLEncoding.EncodeToString([]byte(msg))
// Send the message
_, err_gms := gmailService.Users.Messages.Send("me", &message).Do()
if err_gms != nil {
log.Printf("Error: %v", err_gms)
} else {
err_upd := updateStatus(recipientsList[indx].UUID)
if err_upd {
fmt.Println("Message sent!")
//fmt.Println("The GMAIL response Object Details", gmailResponse)
} else {
fmt.Println("Message sent! But user not updated")
}
}
}
}
}
}
}
答案 2 :(得分:0)
我找到了自己的答案,问题是createTextNode无法更新,所以我只需删除span并再次创建新节点,如下所示:
function validateName() {
var inputName = document.getElementById('name');
var pesanError ='';
if (inputName.value ===""){
remove('name');
pesanError = 'Nama tidak boleh kosong';
tambahError('name',pesanError)
}else if(inputName.value !="" && inputName.value.length < 3){
remove('name');
pesanError = 'Nama yang anda masukkan terlalu pendek'
tambahError('name',pesanError)
}else{
remove('name')
}
}