这是javascript的代码
<script type="text/javascript">
$('.go-btn').click(function analyzeColor3(myColor) {
if (myColor == "Blue" && "Red") {
alert("Just like the sky!");
}
else if (myColor == "Green" && "Black") {
alert("Just like shiraz!");
}
else {
alert("Suit yourself then...");
}
});
这是HTML代码
<h3>Favorite Color</h3>
<select>
<option name="fav_color3" value="Blue"> Blue <br /></option>
<option name="fav_color3" value="Green"> Green <br /></option>
</select>
<h3>Favorite Color</h3>
<select>
<option name="fav_color3" value="Black"> Black <br /></option>
<option name="fav_color3" value="Red"> Red <br /></option>
</select>
<button class="go-btn" onclick="analyzeColor3(this.value);" >
go</button>
答案 0 :(得分:1)
您的代码段包含很多错误,我就如何处理它做了Fiddle,如果您对此有任何疑问,请随时提出。
一些错误:
myColor
analyzeColor3
myColor == "Blue" && "Red"
将无效,你必须像myColor == "Blue" && myColor == "Red"
onclick
两次,一次就足够了。 $('.go-btn').click(function() {
var color1 = $("#color1").val();
var color2 = $("#color2").val();
if (color1 == "Blue" && color2 == "Red") {
alert("Just like the sky!");
}
else if (color1 == "Green" && color2 == "Black") {
alert("Just like shiraz!");
}
else {
alert("Suit yourself then...");
}
})