我对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>
您可以检查年度,制造和建模的值,并返回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('');
});
我认为这样做比在语句中封装所有代码要好得多。
更多提示:
考虑缓存多次使用的变量:
var $ year = $('#year -input'(, $ make = $('#makeInput'(, $型号= $('#modelInput'(;
//现在,将它们用作:$ Year.Val((;
添加一个普通类,在所有字段上说"字段",并将其清除为:
$("。字段"(。val("(;