本文目录一览:
- 1、JS在文本框中输入数据后,自动计算总合,并且不刷新页面的代码
- 2、javascript自动计算金额(急)
- 3、两数相加的总和JS自动计算代码怎么写?
- 4、请帮忙给我写下面表格在HTML5中用JS代码计算小计和总金额的程序代码!谢谢了!急急急急急急!!!!!!
- 5、js表单计算金额问题
JS在文本框中输入数据后,自动计算总合,并且不刷新页面的代码
html
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
title无标题/title
script type="text/javascript"
function sum(obj) {
var z = document.getElementById("z");
var a = document.getElementById("a");
var b = document.getElementById("b");
var c = document.getElementById("c");
all.value=parseInt(z.value);
if(a.value!='')
{
y.value=parseInt(a.value);
h.value=parseInt(z.value)-parseInt(a.value);
}
if(a.value!=''b.value!='')
{
y.value=parseInt(b.value)+parseInt(a.value);
h.value=parseInt(z.value)-parseInt(a.value)-parseInt(b.value);
}
if(a.value!=''b.value!=''c.value!='')
{
y.value=parseInt(b.value)+parseInt(a.value)+parseInt(c.value);
h.value=parseInt(z.value)-parseInt(a.value)-parseInt(b.value)-parseInt(c.value);
}
}
/script
/head
body
总分:input type="text" id="z" οnkeyup="sum(this);" /
input type="text" id="a" οnkeyup="sum(this);" /
input type="text" id="b" οnkeyup="sum(this);" /
input type="text" id="c" οnkeyup="sum(this);" /
总分:input type='text' id='all' style="border:0px solid white; width:25px" /
已选:input type='text' id='y' style="border:0px solid white; width:25px" /
还剩:input type='text' id='h' style="border:0px solid white; width:25px" /
/body
/html
扩展资料
js实现input的赋值
input id="name1" name="teacherName" type="text" /
$('#name1').val('值');
document.getElementById('name1').value='值';
document.getElementById('name1').html('值');
document.getElementById('name1').attr('值');
document.getElementById('name1').innerText = '值';
input class="easyui-textbox" id="name" name="teacherName" type="text" /
$('#name').textbox('setValue','值');
var val = $("#name").textbox('getValue')
javascript自动计算金额(急)
你的补充问题错在这句sum[i]=amount[i]*price[i];
改成这样sum[i].value=amount[i].value*price[i].value;
两数相加的总和JS自动计算代码怎么写?
我需要得到js实现两个数字相加的结果,可是js总是默认按照字符串处理,将两个数字连接起来。
解决这个问题,代码如下:
script type="text/javascript" var a = 1; var b = 2; var sum = parseInt(a) + parseInt(b); /script
1
2
3
4
5
parseInt()是js的解析字符串的函数,返回解析后的数字。
请帮忙给我写下面表格在HTML5中用JS代码计算小计和总金额的程序代码!谢谢了!急急急急急急!!!!!!
!DOCTYPE HTML
html
head
meta charset="utf-8"
titleDetails/title
style
*{
margin:0;
}
table{
margin:auto;
width: 800px;
border: 1px solid black;
border-collapse:collapse;
}
caption{
text-align:left;
font: bolder 22px consolas;
}
th{
background-color:red;
}
th,td{
text-align:center;
border:1px solid black;
}
tdinput[type=text]{
width:50px;
text-align:center;
}
/style
script
onload = function(){
var tbody = tab.tBodies[0];
var rows = tbody.rows;
var texts = tbody.getElementsByTagName("input");
for(var i = 0; i texts.length; i++){
var ti = texts[i];
ti.onkeyup = function(){
var me = this;
var dom = me.parentElement.nextElementSibling;
var price = dom.innerHTML.replace(/[^\d\.]/g,"");
var total = dom.nextElementSibling;
total.innerHTML = "$" + (me.value * price).toFixed(2);
var t = 0;
for(var i = 1; i rows.length - 1; i++){
var td = rows[i].cells[4].innerHTML.replace(/[^\d\.]/g,"");
t += td * 1;
}
sum.innerHTML = "$" + t.toFixed(2);
}
}
}
/script
/head
body
table id="tab"
captionOrder Details/caption
tr
thProduct Code/th
thDecription/th
thQty/th
thPrice/th
thTotal/th
/tr
tr
tdCOMP001/td
tdThe Ultimate Smartphone/td
tdinput type="text" //td
td$399.99/td
td$0.00/td
/tr
tr
tdCOMP002/td
tdThe Ultimate Tablet/td
tdinput type="text" //td
td$299.99/td
td$0.00/td
/tr
tr
tdCOMP003/td
tdThe Ultimate Netbook/td
tdinput type="text" //td
td$499.99/td
td$0.00/td
/tr
tr
td colspan=4 style="text-align:right;"Order Total/td
td id="sum"$0.00/td
/tr
/table
/body
/html
js表单计算金额问题
我把你那个改成表格的了~这样比较好实现~代码如下:
table width="600" border="1" align="center" style="text-align:center;"
tr
td width="102"input type="checkbox" id="all"/全选/td
td width="203"商品/td
td width="119"单价/td
td width="148"数量/td
/tr
tr
tdinput type="checkbox" id="qianbi"//td
td铅笔/td
td1.5元/支/td
tdinput type="text" id="qianbi_t" value="1" //td
/tr
tr
tdinput type="checkbox" id="yuanzhu" value="" //td
td圆珠笔/td
td2.5元/支/td
tdinput type="text" id="yuanzhu_t" value="1" //td
/tr
tr
tdinput type="checkbox" id="gangbi" value="" //td
td钢笔/td
td3元/支/td
tdinput type="text" id="gangbi_t" value="1" //td
/tr
tr
td colspan="4"金额合计:span id="m" style="color:#F00;"/span元nbsp;nbsp;nbsp;nbsp;button提交订单/button/td
/tr
/table
script type="text/javascript"
var all=document.getElementById("all");
var qianbi=document.getElementById("qianbi");
var yuanzhubi=document.getElementById("yuanzhu");
var gangbi=document.getElementById("gangbi");
var qb_m=0,yzb_m=0,gb_m=0;
all.onclick=function(){
if(all.checked){
qianbi.checked=true;
yuanzhubi.checked=true;
gangbi.checked=true;
}
else{
qianbi.checked=false;
yuanzhubi.checked=false;
gangbi.checked=false;
}
sub();
};
qianbi.onclick=function(){sub()};
yuanzhubi.onclick=function(){sub()};
gangbi.onclick=function(){sub()};
document.getElementById("qianbi_t").addEventListener("change",function(e){
sub();
});
document.getElementById("yuanzhu_t").addEventListener("change",function(e){
sub();
});
document.getElementById("gangbi_t").addEventListener("change",function(e){
sub();
});
function sub(){
if(qianbi.checked){
qb_m=document.getElementById("qianbi_t").value*1.5
}
else{
qb_m=0;
all.checked=false;
}
if(yuanzhubi.checked){
yzb_m=document.getElementById("yuanzhu_t").value*2.5
}
else{
yzb_m=0;
all.checked=false;
}
if(gangbi.checked){
gb_m=document.getElementById("gangbi_t").value*3
}
else{
gb_m=0;
all.checked=false;
}
var sum=qb_m+yzb_m+gb_m;
document.getElementById("m").innerHTML=sum;
}
/script
复制到网页的body中去就行了~试试看~不懂得再来问我~