javascript - 为什么下面的按钮 switch case javascript 不起作用?

标签 javascript jquery html ibm-mobilefirst

我使用以下代码来获取按钮用户单击的值,然后在标签中打印相同的值。我已经在 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/

相关文章:

javascript - 查找在 HTML 5 Canvas 上绘制的不规则形状的最大高度宽度

javascript - 多次捕获两个相同单词之间的所有内容

javascript - 在 jQuery 函数调用中附加 id?

javascript - 如何每隔几秒在php中刷新一个具有打印语句和图像功能的div

jquery - 在 ajax 中发布时出现 Access-Control-Allow-Origin 错误

html - Bootstrap 中的垂直对齐

javascript - 单击 href 时添加额外的 html 表格行

javascript - 在 ReactJs 中创建动态 svg 图标组件

javascript - 替换为javascript中的html标签

javascript - 使用 Greasyspoon 将脚本插入网站的头部