不推送数组的表单验证

Form validation without pushing array

我是 JQuery 的新手,我的问题是我有 3 个输入字段来输入有关汽车的信息(年份、品牌、型号),如果填写了这些字段,则为年份、品牌模型被推入 garage 数组。但是,由于我的代码当前是在未填写字段且单击 Add Car 按钮的情况下编写的,因此会将一个空字符串推送到数组。我的作业特别指出,如果用户在字段为空时单击按钮,则不要推送空数组。

<body>
<div id="contents">
  <h1>Week 6 Prework Assignment</h1>
  <div id="input">
      <input placeholder="Year" id="yearInput"/>
      <input placeholder="Make" id="makeInput"/>
      <input placeholder="Model" id="modelInput"/>
      <button type="submit" id="addCarButton">Add Car Button</button>
    <ul id='carList'>
    </ul>
  </div>
</div>

class Car{
  constructor( year, make, model ){
    this.year = year;
    this.make = make;
    this.model = model;
  } //end constructor
} // end Car class

let garage = [];

function newCar( year, make, model ){
  console.log( 'in newCar:', year, make, model );
  garage.push( new Car( year, make, model ) );
  return true;
}

// on document load
$( document ).ready(function(){
  $('#addCarButton').on('click', function newCar(){
    // declare new variable 'addedCar' and set equal to value of year, make and model inputs
    let addedCar = new Car( $( '#yearInput' ).val(), $( '#makeInput' ).val(), $( '#modelInput' ).val() );
    // push 'addedCar' variable to 'garage' array
    garage.push( addedCar );
    // run 'updateCars' function
    updateCars();
    // run 'textRequired' function
    textRequired();
    // empty year, make, model inputs
    $('#yearInput').val( '' );
    $('#makeInput').val( '' );
    $('#modelInput').val( '' );
  });
}); //end ready document

function updateCars(){
  console.log('in updateCars');
  // loop through and display cars on DOM
  let carOutput = $('#carList');
  carOutput.empty();
  for(let car of garage ){
    carOutput.append( '<li>' + car.year + ' ' + car.make + ' ' + car.model + '</li>');
  } // end for of loop
} // end updateCars

function textRequired(){
  if($('#yearInput').val()==='' || $('#makeInput').val()==='' || $('#modelInput').val()===''){
    alert('Please enter text in the required fields');
  }
}

if 语句将在推入数组之前检查值,如果任何输入为空,则不会发生任何事情

class Car{
  constructor( year, make, model ){
    this.year = year;
    this.make = make;
    this.model = model;
  } //end constructor
} // end Car class

let garage = [];

function newCar( year, make, model ){
  console.log( 'in newCar:', year, make, model );
  garage.push( new Car( year, make, model ) );
  return true;
}

// on document load
$( document ).ready(function(){
  $('#addCarButton').on('click', function newCar(){
  if($( '#yearInput' ).val() && $( '#makeInput' ).val() && $( '#modelInput' ).val()){
   let addedCar = new Car( $( '#yearInput' ).val(), $( '#makeInput' ).val(), $( '#modelInput' ).val() );
    // push 'addedCar' variable to 'garage' array
    garage.push( addedCar );
    // run 'updateCars' function
    updateCars();
    // run 'textRequired' function
    textRequired();
    // empty year, make, model inputs
    $('#yearInput').val( '' );
    $('#makeInput').val( '' );
    $('#modelInput').val( '' );
     }
  });
 
    // declare new variable 'addedCar' and set equal to value of year, make and model inputs
   
}); //end ready document

function updateCars(){
  console.log('in updateCars');
  // loop through and display cars on DOM
  let carOutput = $('#carList');
  carOutput.empty();
  for(let car of garage ){
    carOutput.append( '<li>' + car.year + ' ' + car.make + ' ' + car.model + '</li>');
  } // end for of loop
} // end updateCars

function textRequired(){
  if($('#yearInput').val()==='' || $('#makeInput').val()==='' || $('#modelInput').val()===''){
    alert('Please enter text in the required fields');
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="contents">
  <h1>Week 6 Prework Assignment</h1>
  <div id="input">
      <input placeholder="Year" id="yearInput"/>
      <input placeholder="Make" id="makeInput"/>
      <input placeholder="Model" id="modelInput"/>
      <button type="submit" id="addCarButton">Add Car Button</button>
    <ul id='carList'>
    </ul>
  </div>
</div>

您可以检查年份、品牌和型号的值,如果其中一个为空,则 return false。

这可以按如下方式完成:

$('#addCarButton').on('click', function newCar() {
    if (!$('#yearInput').val() || !$('#makeInput').val() || !$('#modelInput').val()) {
        return false;
    }
    // declare new variable 'addedCar' and set equal to value of year, make and model inputs
    let addedCar = new Car($('#yearInput').val(), $('#makeInput').val(), $('#modelInput').val());
    // push 'addedCar' variable to 'garage' array
    garage.push(addedCar);
    // run 'updateCars' function
    updateCars();
    // run 'textRequired' function
    textRequired();
    // empty year, make, model inputs
    $('#yearInput').val('');
    $('#makeInput').val('');
    $('#modelInput').val('');
});

我认为这样做比将所有代码封装在一个 if 语句中要好得多。

更多提示:

  1. 考虑缓存被多次使用的变量:

    var $year = $('#yearInput'), $make = $('#makeInput'), $model = $('#modelInput');

    //现在,将它们用作: $year.val();

  2. 在所有字段上添加一个公共 class,说 "field" 并将它们清除为:

    $(".field").val("");