if语句即使console.log确认也无法识别值

时间:2019-02-06 18:01:25

标签: javascript html if-statement textfield getelementbyid

我正在尝试用HTML制作联系人列表。添加新联系人时,会出现一个菜单,用户可以在其中输入有关该联系人的信息。由于某些原因,当我尝试console.log记录任何字段的值时(由于我需要使用firstnamefield字段来确定联系人是否有效),它表示找不到null值。结果,我的if语句不起作用,并且无法阻止用户进行完全空白的联系。

下面的代码,感谢您的帮助。

function addContact() { //called by the "add contact button", inserts the fields for the custom
      document.getElementById("description").innerHTML="<h2><input type='text' id='firstnamefield' placeholder='First Name' value=''></h2><h2><input type='text' id='middlenamefield' placeholder='Middle Name'></h2><h2><input type='text' id='lastnamefield' placeholder='Last Name'></h2><input type='text' id='nicknamefield' placeholder='Nickname'><br><br><input type='text' id='phonefield' placeholder='Phone'><br><input type='text' id='emailfield' placeholder='Email'><br><br><input type='text' id='birthdayfield' placeholder='Birthday'><br><button type='button' id='finishbutton' onclick='finalizeContact()'>Finished</button>";
}

function finalizeContact() { //called by the "finished" button on the contact information form that appears when adding a contact, turns the information that has just been input by the user into a contact under the custom section at the bottom of the list
  var firstname=document.getElementById('firstnamefield').value;
  console.log(firstname);
  if(document.getElementById("firstnamefield").value != null || firstname != '') {
    document.getElementById('custom').innerHTML = document.getElementById('custom').innerHTML + "<button type='button' class='customcontact' onclick=\"display('" + document.getElementById('firstnamefield').value + "', '" + document.getElementById('nicknamefield').value + "', '" + document.getElementById('lastnamefield').value + "', '" + document.getElementById('nicknamefield').value + "', '" + document.getElementById('phonefield').value + "', '" + document.getElementById('emailfield').value + "', '" + document.getElementById('birthdayfield').value + "')\">" + document.getElementById('firstnamefield').value + " " + document.getElementById('lastnamefield').value + "</button>";
    display(document.getElementById('firstnamefield').value, document.getElementById('middlenamefield').value, document.getElementById('lastnamefield').value, document.getElementById('nicknamefield').value, document.getElementById('phonefield').value, document.getElementById('emailfield').value, document.getElementById('birthdayfield').value)
    if(document.getElementById('custom').style.display=="none") { // if there hasn't been a custom contact created yet, the CUSTOM section isn't visible. therefore, if the CUSTOM section is hidden, this function will make it visible
      document.getElementById('custom').style.display="block";
    }
  } else {
    document.getElementById("firstnamefield").style.border = "5px solid red";
    document.getElementById("description").innerHTML = "Please enter a name.<br>" + document.getElementById("description").innerHTML;
  }
}

0 个答案:

没有答案