gpt4 book ai didi

javascript - 动态创建的范围形式,只有最后一个范围有效,为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 16:14:26 25 4
gpt4 key购买 nike

我最近试图动态生成一些范围形式,但我陷入了困境,只有最后一个范围有效,即使它们有不同的 id。这是为什么 ?我认为它不喜欢我的全局 rangenum 变量,但为什么?

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>

var rangenum = 0;
var rangediv = new Array();
var rangeform = new Array();


var range = {};
range['position'] = {
controls : [{ pos: "parent",
inc: 1,
out: "oX",
id: "x",
label: 35,
vis: "block",
step: 0.1,
min: -100,
max: 100,
def: 0 },
{ pos: "parent",
inc: 1,
out: "oY",
id: "y",
label: 36,
vis: "block",
step: 0.1,
min: -100,
max: 100,
def: 0 }
]
}

function rangebox(obj) {

prnt = document.getElementById(obj.pos);
rangenum++;


rangediv[obj.id] = document.createElement("DIV");

rangediv[obj.id].id='div_'+obj.id;
rangeform[obj.id] = document.createElement("FORM");

rangeform[obj.id].oninput= function() {
input=document.getElementById('i'+rangenum).value;
document.getElementById('o'+rangenum).value=input;
console.log(input);
}

rangeoutput = document.createElement("OUTPUT");
rangeoutput.id = 'o'+rangenum;
rangeoutput.value = obj.def;
rangeoutput.innerHTML=obj.def;

rangeinput = document.createElement("INPUT");
rangeinput.type='range';
rangeinput.step=obj.step;
rangeinput.min=obj.min;
rangeinput.max=obj.max;
rangeinput.value=obj.def;
rangeinput.id = 'i'+rangenum;
rangeinput.innerHTML=obj.def;

prnt.appendChild(rangediv[obj.id]);



rangediv[obj.id].appendChild(rangeform[obj.id]);
rangeform[obj.id].appendChild(rangeoutput);
rangeform[obj.id].appendChild(rangeinput);
}

function getrange () {
i=0;
while (range['position'].controls[i]) {
rangebox(range['position'].controls[i]);
i++;
}
}




</script>

<div id="parent"></div>


<a href="javascript:void(0);" onClick="getrange()">get range</a>


</body>
</html>

最佳答案

在你的代码中rangenum是问题所在,试试这个

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
</head>

<body>
<script>

var rangenum = 0;
var rangediv = new Array();
var rangeform = new Array();


var range = {};
range['position'] = {
controls : [{ pos: "parent",
inc: 1,
out: "oX",
id: "x",
label: 35,
vis: "block",
step: 0.1,
min: -100,
max: 100,
def: 0 },
{ pos: "parent",
inc: 1,
out: "oY",
id: "y",
label: 36,
vis: "block",
step: 0.1,
min: -100,
max: 100,
def: 0 }
]
}

function rangebox(obj) {

prnt = document.getElementById(obj.pos);
rangenum++;


rangediv[obj.id] = document.createElement("DIV");

rangediv[obj.id].id='div_'+obj.id;
rangeform[obj.id] = document.createElement("FORM");

rangeform[obj.id].oninput= function() {
console.log(document.activeElement.id.slice(1,2));
var idval=document.activeElement.id.slice(1,2)
input=document.getElementById('i'+idval).value;
document.getElementById('o'+idval).value=input;
// console.log(input);
}

rangeoutput = document.createElement("OUTPUT");
rangeoutput.id = 'o'+rangenum;
rangeoutput.value = obj.def;
rangeoutput.innerHTML=obj.def;

rangeinput = document.createElement("INPUT");
rangeinput.type='range';
rangeinput.step=obj.step;
rangeinput.min=obj.min;
rangeinput.max=obj.max;
rangeinput.value=obj.def;
rangeinput.id = 'i'+rangenum;
rangeinput.innerHTML=obj.def;

prnt.appendChild(rangediv[obj.id]);



rangediv[obj.id].appendChild(rangeform[obj.id]);
rangeform[obj.id].appendChild(rangeoutput);
rangeform[obj.id].appendChild(rangeinput);
}

function getrange () {
i=0;
while (range['position'].controls[i]) {
rangebox(range['position'].controls[i]);
i++;
}
}




</script>

<div id="parent"></div>

<a href="javascript:void(0);" onClick="getrange()">get range</a>


</body>

</html>

关于javascript - 动态创建的范围形式,只有最后一个范围有效,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41183519/

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