addEventListener onClick无法正常工作

时间:2013-02-06 13:25:56

标签: javascript function addeventlistener

我不知道我的代码有什么问题,但是当我使用addEventListener()时,它没有做任何事情。

有没有人有另一个解决方案,以便我可以将#ibm按钮ID绑定到compute()onClick事件?

我打算在Firfox OS上运行它,因此禁止在按钮上设置onClick属性,如CSP Violations所述。

    function compute()
    {
var w = parseInt(document.getElementById('weight').value);
var hf = parseInt(document.getElementById('height_ft').value);
var hi = parseInt(document.getElementById('height_in').value);

    if (!checker(w))
{

            showAndroidToast('Please input your weight');
    return;
}
else if (!checker(hf))
{

            showAndroidToast('Please input your height');
    return;
}


if (!checker(hi))
{
    var i = 0;
    document.getElementById('height_in').value = i;
}
else
{
    var i = parseInt(document.getElementById('height_in').value);
}

var comp_h = parseFloat((hf * 12) + (i * 1));
var tot_h = comp_h * comp_h;    

comp = formula(w, tot_h);
document.getElementById('bmitot').value = comp;

if (comp > 30)
{
    document.getElementById('res').value = 'Severe Level! Thats too much FAT!      Go get some diet pills or something!';
    document.getElementById("bmitot").style.backgroundColor = "red";
    document.getElementById("zero").style.visibility = "hidden";
    document.getElementById("one").style.visibility = "hidden";
    document.getElementById("two").style.visibility = "hidden";
    document.getElementById("three").style.visibility = "visible";
}
if (comp > 25 && comp <=29.9)
{
    document.getElementById('res').value = 'Bad Level! You are too FAT!';
    document.getElementById("bmitot").style.backgroundColor = "yellow";
    document.getElementById("zero").style.visibility = "hidden";
    document.getElementById("one").style.visibility = "hidden";
    document.getElementById("two").style.visibility = "visible";
    document.getElementById("three").style.visibility = "hidden";
}
if (comp > 18.5 && comp <=24.9)
{
    document.getElementById('res').value = 'Nice! You are on the safe level!';
    document.getElementById("bmitot").style.backgroundColor = "green";
    document.getElementById("zero").style.visibility = "hidden";
    document.getElementById("one").style.visibility = "visible";
    document.getElementById("two").style.visibility = "hidden";
    document.getElementById("three").style.visibility = "hidden";
}
if (comp < 18.5)
{
    document.getElementById('res').value = 'Bad Level! Go get some nutrients!';
    document.getElementById("bmitot").style.backgroundColor = "yellow";
    document.getElementById("zero").style.visibility = "visible";
    document.getElementById("one").style.visibility = "hidden";
    document.getElementById("two").style.visibility = "hidden";
    document.getElementById("three").style.visibility = "hidden";
}
}

function formula(w, h)
{
var bmi = w/h * 703;
var a_bmi = Math.floor(bmi);
var dif = bmi - a_bmi;
dif = dif * 10;
diff = Math.round(dif);

if (dif == 10)
{
    a_bmi += 1;
    dif = 0;    
}
bmi = a_bmi + "." + diff;
return bmi;
}

function checker(type)
{
  if (isNaN(parseInt(type)))
{
  return false;
} 
else if (type < 0)
{
  return false;
}
else
{
return true;
}
}

function showAndroidToast(toast) 
{
   Android.showToast(toast);
}

 document.getElementById('ibm').addEventListener('click', compute);

1 个答案:

答案 0 :(得分:0)

您的代码执行正常,计算功能正在触发。 execute函数中的代码可能存在问题。请检查此fiddle