javascript函数不起作用

时间:2014-05-06 12:14:26

标签: javascript

我会改述我的问题

我写了这段代码但我不明白为什么它不起作用......

我想创建一个人来到网站订购三明治他写的是我在网站上写的选项之一,然后显示他选择的三明治和三明治中的成分(成分都是数组)

我也会添加我的html代码,如果有人可以为正确的代码编写示例,那么对我来说会更有帮助。

提前多多感谢!

JavaScript代码:

var avucado =  ["eggs ","avucado ","tommato ","mayonnaise ","pickles ","gamba"];
var eggSalad =  ["eggs ","dill ","mayonnaise ","pickles"];
var tuna =   ["tuna ","coriander ","pickles ","mayonnaise ","gamba"];
var cheeze = ["yellow cheeze ","mayonnaise ","tommato ","cucumber"];
var sausage =["sausage ","mustard ","mayonnaise ","tommato ","pickles"];
var choice1 = ("you chose:");
var choice2 = ("the ingredients of your sandwich are:") 



var food = function(foodName)
{

if (document.getElementById("input").value = avucado) {

    document.write(choice1 + " " + 'avucado.'+ "<br /><br />" + " " + choice2 + 
    " " + avucado + ".");


}else if(document.getElementById("input").value = eggSalad) {

    document.write(choice1 + " " + 'egg salad.'+ "<br /><br />" + " " + choice2 + 
    " " + eggSalad + ".");


}else if(document.getElementById("input").value = tuna) {

    document.write(choice1 + " " + 'tuna.'+ "<br /><br />" + " " + choice2 + 
    " " + tuna + ".");


}else if(document.getElementById("input").value = cheeze) {

    document.write(choice1 + " " + 'yellow cheeze.'+ "<br /><br />" + " " + choice2 + 
    " " + cheeze + ".");


}else if(document.getElementById("input").value = sausage) {

    document.write(choice1 + " " + 'tuna.'+ "<br /><br />" + " " + choice2 + 
    " " + tuna + ".");
}

else(document.getElementById("input".value != avucado,eggSalad,tuna,cheeze,sausage){

    document.write("Error, please chose one of the sandwich's on the list");
    }


};

HTML code:

<html>
<head>
<title>Hello World!</title>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="script.js"> </script>


</head>

<body dir="rtl">
<h1> wich sandwich would you like to order? </h1>
<form>
<input type="text" id="input" /><br />
<button type="button" id="button" value="submit" onclick="food()">submit</button><br />

</form>


</body>

</html>

1 个答案:

答案 0 :(得分:2)

替换所有出现的:

document.getElementById("input").value =

使用:

document.getElementById("input").value ==

单身&#34;等于&#34;标志(=)是"Assignment" operator.
双&#34;等于&#34;签名(==)是"Comparison" operator.

if中,您正在分配食品&#39;内容到输入.value,而不是检查是否相等。

要检查value是否在数组中,您应该执行以下操作:

if(avucado.indexOf(document.getElementById("input").value) !== -1){

因此,如果值在数组avucado中,那么请执行if下面的内容。

最后,您的else声明需要一些修复:

else(document.getElementById("input".value != avucado,eggSalad,tuna,cheeze,sausage){
    document.write("Error, please chose one of the sandwich's on the list");
}

将其替换为:

else if(avucado.concat(eggSalad,tuna,cheeze,sausage).indexOf(document.getElementById("input").value) == -1){
    document.write("Error, please chose one of the sandwich's on the list");
}

现在,我建议使用DOM操作,而不是document.write("text")

在HTML中,添加如下元素:

<span id="message"></span>

然后,在你的JS:

document.getElementById("message").innerHTML = "text";