IF 语句并返回到函数的开头

IF statements and returning to the start of a function

我正在尝试将表单中的数据输入到数组中, 一切都很好,但我注意到即使在提交空输入时它仍然将它记录到我的数组中, 我正在尝试执行 IF 语句来检查输入字段 "Name" 是否为空,然后重新启动函数并提醒他们它为空

现在它在做相反的事情,当它不是反之亦然时提醒他们它是空的, 我让它以另一种方式工作但最终丢失了那个保存并忘记了我是如何做到的,我也不知道如何重新启动该功能(以避免它被添加到数组中) 这是我的代码,

function addRecord(){   
  employee.name = document.getElementById('name').value; 
  employee.age = document.getElementById('age').value;
  employee.position = document.getElementById('position').value;
  if(name.length == 0){
    alert('Do you not have a name, mate?');
    return false;
  }  
  employeeList.push(employee);
  employee = new Object();
  writeRecords();
}

谢谢!

尝试这样的事情:

if(!employee.name){
  alert('Do you not have a name, mate?');
  return
}

您也可以使用此逻辑来检查年龄和职位。

希望这就是您所期望的。

var employeeList = [];
function addRecord(){

var employee = {}
 employee.name = document.getElementById('name').value;


 employee.age = document.getElementById('age').value;


 employee.position = document.getElementById('position').value;


 if(!(employee.name && employee.age && employee.position)  ){

alert('The form is incomplete. Please revisit.');
return false;



}

 employeeList.push(employee);
 employee = new Object();
 resetForm();
console.log(employeeList)
}

function resetForm(){

 document.getElementById('name').value = '';
 document.getElementById('age').value = '';
 document.getElementById('position').value = '';
 }
<input id='name'/>
<input id='age'/>
<input id='position'/>
<button onclick='addRecord()'>Add Employee</button>