我有产品名称(在段落中)、文本形式和按钮。产品的 id 如下 p1,p2... 输入类型的 id 如下 i1,i2... 在表单中输入内容并单击提交后,我希望这可以更改默认的产品文本。我有以下功能,仅适用于一组(段落、表单和按钮)。问题是这个乘积函数只适用于 p1,i1 我希望它能够 fork p1,i1,p2,i2 等。
function product(id){
var userInput = document.getElementById("i1").value;
document.getElementById("p1").innerHTML = userInput;
}
函数调用如下:
<button type='button' onclick='product()'>Name product</button>
最佳答案
您需要的是将索引传递给函数:
function product(id) {
var userInput = document.getElementById("i"+id).value;
document.getElementById("p"+id).innerHTML = userInput;
}
你的 html 看起来像:
<button type='button' onclick='product(1)'>Name product</button>
希望这有帮助。
关于Javascript 相关的输入按钮文本更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6435720/