gpt4 book ai didi

javascript - 使用Javascript动态设置onKeyUp属性

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

我目前正在编写JS 代码来计算数字输入的运行总数。我遇到的问题是,对于我的实际项目,我无法直接更改 HTML,因为它是从脚本程序生成的。不过我可以用 JS 来操作它。

到目前为止,我已经添加了一个 div,它可以显示输入的总和。但这仅在输入具有 onKeyUp() 属性时才有效。对于生成的 HTML 来说,情况并非如此,因此我需要编写一个 JS 脚本 将其添加进去。这就是我到目前为止所做的:

<html>
<head>
<script>
//try to set an onKeyUp attribute to the elements.
var number1 = document.getElementById("_Q0_Q0_Q0");
number1.onkeyup = function(){ return summer()};

var number2 = document.getElementById("_Q0_Q0_Q1");
number2.onkeyup = function(){ return summer()};

var number3 = document.getElementById("_Q0_Q0_Q2");
number3.onkeyup = function(){ return summer()};

function summer() {
var count = 0; //start with nothing
var num1 = (parseFloat(document.getElementById("_Q0_Q0_Q0").value)) || 0; //if there aren't any numbers, add nothing
var num2 = (parseFloat(document.getElementById("_Q0_Q0_Q1").value)) || 0;
var num3 = (parseFloat(document.getElementById("_Q0_Q0_Q2").value)) || 0;
count = num1+num2+num3; //sum them up
if (!document.getElementById("output")) { //check to see if there is a div with the output already, if not create one.
var newDiv = document.createElement('div');
newDiv.setAttribute('id', 'output');
var gutterDiv = document.getElementById("right_gutter")
gutterDiv.appendChild(newDiv);
};
document.getElementById("output").innerHTML = "Your running total = "+count
}; //show output
</script>
</head>
<body>
<input type="text" id="_Q0_Q0_Q0" name="number" />
<input type="text" id="_Q0_Q0_Q1" name="number" />
<input type="text" id="_Q0_Q0_Q2" name="number" />
<div id='right_gutter'>
<button type="button" onclick="summer()">Clicking here adds your input to the "count" variable</button> //two purposes of this. 1) Acts as a placeholder for where the output should end up. 2) Shows that the summing function works.
</div>
<br>
</body>
</html>

最佳答案

很高兴你成功了。我想这现在是多余的,但我发布这个答案以防它有用。

//Create a NodeList of all elements whose name='number':
var nums = document.getElementsByName("number");

//add the onkeyup event to all nums:
for(var n=0; n < nums.length; n++){
nums[n].onkeyup = function(){return summer()};
}

//add the click event to the button:
document.getElementById("button").onclick = function(){return summer();}


function summer() {
var count = 0;
for(var n=0; n < nums.length; n++){//iterate and sum values
count += parseFloat(nums[n].value) || 0;
}
//create the output element:
var outputDiv = document.getElementById("output") || document.createElement('div');
//if it doesnt already exist append it:
if(!document.getElementById("output")){
outputDiv.setAttribute('id', 'output');
document.getElementById("right_gutter").appendChild(outputDiv);
createdAlready=true;
}
//set the output string:
outputDiv.innerHTML = "Your running total = "+count;
}

演示:http://jsfiddle.net/8BrQE/2/

关于javascript - 使用Javascript动态设置onKeyUp属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20615744/

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