多个文本框传值的问题

<form  name="form1">
<input type="text"  id="text1" />
<button  onclick="myfunction()">输出</button></form>

<form  name="form2">
<input type="text"  id="text2" />
<button  onclick="myfunction()">输出</button></form>

<form  name="form3">
<input type="text"  id="text3" />
<button  onclick="myfunction()">输出</button></form>

上面这些文本框是数据库循环输出的,数量不定。

<input type="text" id="recive" /> //这个文本框只有一个,用来点按钮后接收对应文本框传的值

<script>
function myfunction()
{
document.getElementById("recive").value=document.getElementById("text1").value;  //这里要怎样写才能每个文本框都运行
}   
 </script>
阅读 1.3k
1 个回答

没大看懂你要干嘛,每个文本框都运行是什么意思,是要获取所点击的按钮前的文本内容吗,是的话可以这么写

<form  name="form1">
<input type="text"  id="text1" />
<button  onclick="myfunction(event)">输出</button></form>


function myfunction(event)
{
document.getElementById("recive").value = event.target.previousElementSibling.value;  //这里要怎样写才能每个文本框都运行
}  

还可以在onclick中把对应的文本id传过去,比如

<form  name="form1">
<input type="text"  id="text1" />
<button  onclick="myfunction('text1')">输出</button></form>

function myfunction(id)
{
document.getElementById("recive").value=document.getElementById(id).value;  //这里要怎样写才能每个文本框都运行
} 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题