我遇到一种情况,我需要查看是否选择了某个选项。当我单击按钮时,如果未选择某个选项,则选择框应变为红色并提示我。这没用。我对 if 语句有困难。有什么解决办法吗?
Javascript
$(document).ready(function(){
var $films = [];
$("#button").click(function(){
var $titelBetyg = [];
var $titel = $('#name').val();
var $betyg = $('#options').val();
if($titel == ""){
$('#name').css('background-color', 'red');
alert("Fail");
}
else if($betyg == "0"){
$('#options').css('background-color', 'red');
alert("Fail");
}
else{
$titelBetyg.push($titel);
$titelBetyg.push($betyg);
$films.push($titelBetyg);
}
$('#rightbar').append($films);
});
});
HTML
<body>
<div id="page">
<div id="header">
<h1> Min filminsamling </h1>
</div>
<div id="leftbar">
<form>
<fieldset>
<legend>Lägg till en film:</legend>
Titel:
<br><input type="text" name="name" id="name"><br>
Betyg:
<br><select id="options">
<option>Välj betyg här...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br><input type="button" name="button" value="Spara film" id="button">
</fieldset>
<fieldset>
<legend>Sortera film:</legend>
<input type="button" name="stigande" value="Högst betyg" id="stigande">
<br><input type="button" name="fallande" value="Lägst betyg" id="fallande">
</fieldset>
</form>
</div>
<div id="rightbar">
Filmer
</div>
</div>
</body>