2013-02-06 74 views
0

我不知道我的代码有什么问题,但是当我使用addEventListener()时,它不会执行任何操作。addEventListener onClick不工作

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

我打算在Firfox操作系统上运行此操作,因此在按钮本身上设置onClick属性是禁止的,如CSP违规中所述。

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); 

回答

0

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

+0

你为什么使用测试?我的代码真的有问题吗?即使当我使用我的浏览器运行代码时,它也不会执行任何操作。 –

+0

我无法真正使用您的代码,因为有像“Android.showToast(toast)”这样的函数不会在我的本地工作,而且我也没有没有完整的html来寻找'weight','height_ft'等等......我需要一个最小的测试用例来检查事件是否正在执行。 –

+0

@MarcusAng他试图创建一个[SSCCE](http://sscce.org/),这是调试编码问题的正确方法。 – jbabey