js获取文本框的值并计算后,如何取得这个值保存在变量里,例如 int price=。。。

[复制链接]
查看11 | 回复5 | 2011-8-19 07:21:23 | 显示全部楼层 |阅读模式
我要做的是商品选择页面,需要放置一个文本框,用户需要在文本框内输入购买的商品数量,我需要在当前页面获取该数量,并且计算总价:单价*数量,将总价显示在当前页面。现在总价计算出来了,如何把计算出得数拿出来用变量保存,因为该数要用sql写入数据库中。简单的以实现的代码如下:htmlheadtitletest/titlescripttype=\"text/javascript\"functionfun(){varprice=document.getElementById(\'price\').value;varcount=document.getElementById(\'count\').value;document.getElementById(\'total\').value=count*price;}/script/headbody单价:inputid=\'price\'type=\'text\'value=\'%=price%\'数量:inputid=\'count\'type=\'text\'onblur=\'fun()\'总额:inputid=\'total\'type=\'text\'/body/html如何将总额total的值保存到变量里?intprice=total?
回复

使用道具 举报

千问 | 2011-8-19 07:21:23 | 显示全部楼层
存到cookie中去,提交到数据库时从cookie中取出来传回服务器functionfun(){varprice=document.getElementById(\'price\').value;varcount=document.getElementById(\'count\').value;document.getElementById(\'total\').value=count*price;document.cookie=\"ctotal=\"count*price;}追问如何从cookie中取出该数据啊?
回复

使用道具 举报

千问 | 2011-8-19 07:21:23 | 显示全部楼层
functiongetCookie(name){vararr=document.cookie.match(newRegExp(\"(^|)\"name\"=([^;]*)(;|$)\"));if(arr!=null)returnunescape(arr[2]);returnnull;}
回复

使用道具 举报

千问 | 2011-8-19 07:21:23 | 显示全部楼层
scripttype=\"text/javascript\"vartotal=0;functionfun(){varprice=document.getElementById(\'price\').value;varcount=document.getElementById(\'count\').value;total=count*price;}/script把total设为全局变量即可追问怎么将var格式的数据保存成常用的数据类型啊
回复

使用道具 举报

千问 | 2011-8-19 07:21:23 | 显示全部楼层
写到数据库的操作是要服务器端程序来做的。就算你把数据保存到了intprice=total等等,都只是前台的数据,没办法写到数据库的。所以,你要把你当前的数量,价格等信息用表单传递给服务器端,asp或者php,看你用哪个了。然后在后台页面里计算出来并插入到数据库中。数据的传递是用表单的。
回复

使用道具 举报

千问 | 2011-8-19 07:21:23 | 显示全部楼层
vartprice=fm.total.value;fm.price.value=tprice;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行