gpt4 book ai didi

javascript - 动态填充表的总和值

转载 作者:行者123 更新时间:2023-11-28 08:59:59 25 4
gpt4 key购买 nike

我正在寻找一种方法来对通过引用全局数组的下拉菜单动态添加到表中的列值求和。下面是我编写的代码,这里是功能示例的链接:jsFiddle 。我是 JavaScript 新手,我所知道的一切都是我自学的,所以请耐心等待,并提前感谢您的帮助。

<!DOCTYPE html>
<html>
<head>
<title> </title>
<script>
var x= ["",1,2,3,4,5,6,7,8,9,10];
var y= ["",4,6,3,4,5,6,7,8,9,10];
var z= ["",1,2,3,4,5,6,7,8,9,10];
</script>
<script>
function fill(){
var si= document.getElementById('ddone').selectedIndex;
if (si!==0){
var a= x[si];
var b= y[si];
document.getElementById('one').innerHTML=a;
document.getElementById('two').innerHTML=b;
}};
</script>
<script>
function fill2(){
var si2= document.getElementById('ddtwo').selectedIndex;
if(si2!==0){
var c=z[si2];
var d=z[si2];
document.getElementById('three').innerHTML=c;
document.getElementById('four').innerHTML=d;
}};
</script>
<script>
function total(){
var w= parseInt(document.getElementById('one').value,10);
var x= parseInt(document.getElementById('two').value,10);
var y=parseInt(document.getElementById('three').value,10);
var z= parseInt(document.getElementById('four').value,10);
document.getElementById('five').innerHTML=w+y;
document.getElementById('six').innerHTML=x+z;
};
</script>
</head>
<body>
<select id='ddone' onchange= 'fill()'>
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<select id='ddtwo' onchange='fill2()'>
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<table border=1px width=25%>
<tr>
<td id='one'></td>
<td id='two'></td>
</tr>
<tr>
<td id='three'></td>
<td id='four'></td>
</tr>
<tr>
<td id='five'></td>
<td id='six'></td>
</tr>
</table>
<button id='button' onclick='total()'>total</button>
</body>
</html>

最佳答案

value 切换为 innerHTML,您的代码将完美运行。请参阅此 fiddle 以获取证明:http://jsfiddle.net/Nt9nC/7/

关于javascript - 动态填充表的总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17887297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com