日期:2014-05-16  浏览次数:20424 次

请教:如何把一个表单中元素的值赋给另一个表单中的元素
如题,表单"form1"中有<select name="select_item">
     表单"form2"中有<input name="status_content">
这两个表单在一个文档中,我想要把"select_item"的值赋给"status_content"

我的实现方法是:
<script language="javascript">
  function get_status(){
    document.form2.status_content.value = document.form1.select_item.value;
  }
</script>
函数的调用方法是:
<form name="form2" action="update_status.php" onsubmit="return get_status()">
.......
</form>
我打出来的"status_content"的值是空,
请教大侠,该怎样正确实现赋值呢?

------解决方案--------------------
经测试你的方法没有问题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
  <form name="form1" ><select name="select_item"><option value='aaa'>aaaa</option></select></form>
 <form name="form2" action="update_status.php" onsubmit="return get_status()"><input name="status_content"><input type='submit' ></form>
  <script type="text/javascript">
  <!--
  function get_status(){
    document.form2.status_content.value = document.form1.select_item.value;
return false;
  }
  //-->
  </script>
 </body>
</html>

------解决方案--------------------
在方法里价格return false值应该赋过去了 只是提交后你看不见了
------解决方案--------------------
如果是在同一个页面中的两个form你这样赋值应该是没有问题的
------解决方案--------------------
先用firefox等工具查看数据post过去没有,没过去就是客户端的问题,过去了就是服务器端的问题, 一步一步排错
------解决方案--------------------
$_REQUEST["status_content"]
------解决方案--------------------
你的提交按钮呢,建议你贴全HTML代码。
------解决方案--------------------
根据你的标题需求写了一段JS,希望对你有用。
<!DOCTYPE html>
<html>
<title>把form1选中的值赋值给form2</title>
<body>
<form name='form1' >
<select id='form_one_select'>
<option value='1'>a</option>
<option value='2'>b</option>
<option value='3'>c</option>
</select>
</form>
<br>
<form name='form2' >
<select id='form_two_select'>
<option value=''></option>
</select>
</form>
</body>
<script>
var selectOne = document.getElementById('form_one_select'),
selectTwo = document.getElementById('form_two_select') ;
selectOne.onchange = function (e){
                // 此处的赋值根据你自己的需求进行调整。