请教老师:
<form name="form1">
<input type="text" id="id1" />
<input type="text" id="text1" />
<input type="text" id="name1" />
<button onclick="myfunction()">输出</button></form>
<form name="form2">
<input type="text" id="id1" />
<input type="text" id="text1" />
<input type="text" id="name1" />
<button onclick="myfunction()">输出</button></form>
<form name="form3">
<input type="text" id="id1" />
<input type="text" id="text1" />
<input type="text" id="name1" />
<button onclick="myfunction()">输出</button></form>
//上面这些文本框是数据库循环输出的,数量不定。
<input type="text" id="recive1" />
<input type="text" id="recive2" />
<input type="text" id="recive3" />
//这里文本框只有三个,用来点每个按钮后接收对应文本框传的值
<script>
function myfunction()
{
document.getElementById("recive").value=document.getElementById("id1").value;
document.getElementById("recive").value=document.getElementById("text1").value;
document.getElementById("recive").value=document.getElementById("name1").value; //这里要怎样写才能每个文本框都运行
}
</script>
这实际上是一个字符串动态拼接的问题。
如果单纯从你给出的代码上修改的话,既然能从数据库循环输出,并且生成
form1
、form2
、form3
...,那么你就应该为文本框生成同样的带标识的id
,并在onclick
方法中也传入该标识:之后让
myfunction()
方法按传入的标识处理即可:因为
<form>
标签中只存在一个<button>
时会自动成为<button type="submit">
,因此点击时会触发提交操作,而默认的提交操作就是会刷新页面。