Javascript在IE中返回Nan,FF ok [英] Javascript returns Nan in IE, FF ok

查看:142
本文介绍了Javascript在IE中返回Nan,FF ok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,并编写此脚本来添加购物车并打印小计和总计。它适用于FF但不适用于IE。通过onclick三个选项之一调用此函数,值为0-25。它位于头部调用的js文件中。它的作用是将选定的值作为变量,解析它们,添加和相乘,并更改表的innerHTML以反映小计和总计。 FF做得很好,但IE给了Nan。香港专业教育学院尝试以多种不同的方式重写它,许多翻译仍然在FF但不是IE8。我确保变量和形式id不重复。

im very new to javascript, and writing this script to add up a shopping cart and print out subtotals and totals. it works in FF but not in IE. this function is called by onclick of one of three select options with a value of 0-25. it is in a js file called in the head. what it does is get the selected values as variables, parseint them, adds and multiplies, and changes the innerHTML of the table to reflect the subtotals, and total. FF does it great, but IE gives Nan. ive tried rewriting it a number of different ways, and many translations still work in FF but not IE8. ive made sure the variables and form id's arent repeated.

function gen_invoice() {
    var scount = parseInt(document.shopcart.studentcount.value, 10);
    var ycount = parseInt(document.shopcart.youthcount.value, 10);
    var fcount = parseInt(document.shopcart.facultycount.value, 10);

    //html output source is 3 selects like this, with diff ids and names: 
    //<select name="studentcount" id="studentcount">  
    //<option onclick="gen_invoice()" value="0">0 </option></select>
    var cardcost = parseInt(document.shopcart.cardprice.value, 10);

    //cardcost comes from hidden input value: 
    //<input type="hidden" id="cardprice" name="cardprice" value="25">
    var totalsum = scount + ycount + fcount;

    var grandtotal = totalsum * cardcost;

    document.getElementById('s_price').innerHTML = scount * cardcost;
    document.getElementById('y_price').innerHTML = ycount * cardcost;
    document.getElementById('f_price').innerHTML = fcount * cardcost;
    document.getElementById('grand').innerHTML = grandtotal;
    //....
}

...之后那里是3个长循环用于写出一些其他形式,但它们不能在IE中工作,因为它们依赖于所选值为整数。这部分首先发生并返回Nan,所以我确定问题就在这里。
我真的在桌子上撞到了我的脑袋。您可以想象能够精美地编写整个网站的其余部分是多么令人沮丧,但是却无法将3个数字加在一起。请帮助!

...after this there are 3 long loops for writing out some other forms, but they dont work in IE either because they depend on the selected values to be an integer. this part happens first and returns Nan, so im sure the problem is here somwhere. I have literally hit my head on the table over this. You can imagine how frustrating it is to be able to write the entire rest of the site beautifully, but then fail at adding 3 numbers together. help please!

推荐答案

使用

    访问表单元素可能会更好运>
  • document.getElementById(foo)

  • document.forms。 shopcart.elements.cardprice

  • document.getElementById("foo")
  • document.forms.shopcart.elements.cardprice

这两种方法都比你现在做的更好。

Both these are better approaches than what you are doing now.

这篇关于Javascript在IE中返回Nan,FF ok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆