如何在字段和新创建的字段之间添加数字。 [英] How to add numbers between a field and a new created field.
问题描述
请在我的工作中有一个问题,我有输入字段,并有一个按钮,我用来创建新的输入字段与onclick事件但我的问题是如何在两个输入字段中添加数字。
我的尝试:
var count = 1;
function add(){
count ++;
var form = document.getElementById('3030')
var divtest = document.createElement (div);
divtest.innerHTML ='< form id =code_3030>< div id =credit_unitname =myfile >< input type =textid =unit4placeholder =Creditonkeyup =mul()>< / div>< / form>';
< br $>
form.appendChild(divtest)
}
函数mul(){
var unit = document.getElementById('unit4')。value,
output = document .getElementById('code4');
if(form.appendChild(divest)== true){
var ans = unit * divtest;
} else {
提醒(解决);
}
output.value = ans;
}
please i have a problem here in my work i have and input field and have a button that i use to create new input field with onclick event but my problem is how to add numbers in both input field.
What I have tried:
var count = 1;
function add(){
count++;
var form = document.getElementById('3030')
var divtest = document.createElement("div");
divtest.innerHTML = '<form id = "code_3030"><div id = "credit_unit" name = "myfile"><input type = "text" id = "unit4" placeholder = "Credit" onkeyup = "mul()"></div></form>';
form.appendChild(divtest)
}
function mul(){
var unit = document.getElementById('unit4').value,
output = document.getElementById('code4');
if(form.appendChild(divest) == true){
var ans = unit * divtest;
}else{
alert("work it out");
}
output.value = ans;
}
推荐答案
你的Add()函数将创建多个div和具有相同id的输入
Your Add() function will create multiple div and inputs with same id
<div id = "credit_unit" name = "myfile">
<input type = "text" id = "unit4" placeholder = "Credit" onkeyup ="mul()">
</div>
<div id = "credit_unit" name = "myfile">
<input type = "text" id = "unit4" placeholder = "Credit" onkeyup ="mul()">
</div>
getElementById将返回更多值节点,您需要循环或采用默认值或创建一个唯一身份。同时删除代码中类型之间的空格,并在javascript中使用双引号而不是单引号。
getElementById will return more value nodes, you need to loop or take the default or create a unique id. Also remove te spaces between the types in your code and use double quotes in your javascript instead of single.
document.getElementById("code4")[0].value;
这篇关于如何在字段和新创建的字段之间添加数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!