我使用以下代码来获取按钮用户单击的值,然后在标签中打印相同的值。我已经在 Worklight 项目中编写了这段代码,当我运行该项目时,我定义的其他函数似乎都不起作用。但是当我把这个功能放在评论中时,一切似乎都工作得很好。
我还尝试过直接将按钮的 id 直接放入开关盒中,但这也不起作用。我对 javascript 的概念很陌生,无法找到解决方案。如果有帮助的话,在 JavaScript 下提供了必要的 html。
function typecheck(objButton) {
exptype = document.getElementById("exptype");
grocery = document.getElementById("button");
medical = document.getElementById("button2");
education = document.getElementById("button3");
entertainment = document.getElementById("button4");
automobile = document.getElementById("button5");
insurance = document.getElementById("button6");
others = document.getElementById("button7");
switch (objButton)
case "grocery":
exptype.innerHTML = "grocery";
break;
case "medical":
exptype.innerHTML = "medical";
break;
case "education":
exptype.innerHTML = "education";
break;
case "entertainment":
exptype.innerHTML = "entertainment";
break;
case "automobile":
exptype.innerHTML = "automobile";
break;
case "insurance":
exptype.innerHTML = "insurance";
break;
default:
exptype.innerHTML = "others";
break;
}
html 代码:
<div data-role="collapsible" id="collapsible2">
<h3>Category</h3>
<div data-role="controlgroup" id="controlgroup1">
<a href="#" data-role="button" id="button" onclick="typecheck(this)">Grocery</a>
<a href="#" data-role="button" id="button2" onclick="typecheck(this)">Medical</a>
<a href="#" data-role="button" id="button3" onclick="typecheck(this)">Education</a>
<a href="#" data-role="button" id="button4" onclick="typecheck(this)">Entertainment</a>
<a href="#" data-role="button" id="button5" onclick="typecheck(this)">Automobile</a>
<a href="#" data-role="button" id="button6" onclick="typecheck(this)">Insurance</a>
<a href="#" data-role="button" id="button7" onclick="typecheck(this)">Others</a>
</div>
<label id="exptype">Label:</label>
最佳答案
试试这个(记住区分大小写):
function typecheck(objButton)
{
switch(objButton.innerHTML){
case "Grocery": exptype.innerHTML= "grocery";
break;
case "Medical": exptype.innerHTML= "medical";
break;
case "Education": exptype.innerHTML= "education";
break;
case "Entertainment": exptype.innerHTML= "entertainment";
break;
case "Automobile": exptype.innerHTML= "automobile";
break;
case "Insurance": exptype.innerHTML= "insurance";
break;
default: exptype.innerHTML= "others";
break;
}
}
关于javascript - 为什么下面的按钮 switch case javascript 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22673675/