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

怎样在js函数中将动态创建的select元素放入已有的表单中进行提交?

我用的是createelement创建的一个select,名字是operator,但是不知道怎样将他加入已有的一个from里面。程序大概是这样的,按下newsearch后,会自动出现一个新的select,用户选择好后,按下submit建,我能构将这两哥select的值一次提交到新的页面。但实际的运行结果是,我只能得到原来静态的那哥select的值。    
下面是源码,多谢高手指教:    
 
 
<html>    
<script     type= "text/javascript ">    
function   addNewSearch()
{
var             NewOperator     =     document.createElement( " <select     name= 'Operator ' "> </select> ");                
NewOperator.length     =     3;    
NewOperator.options[0]     =     new     Option( 'And ',     'and ');        
NewOperator.options[1]     =     new     Option( 'Or ',     'or ');        
NewOperator.options[2]     =     new     Option( 'Not ',     'not ');        
document.body.insertBefore(NewOperator);    
}  
</script>    
 
<form     name= "Search "     action= "FilterResult.php "     method= "post ">    
 
<select     name= "SearchWhat "     onchange     =     "setSearchField(); ">    
              <option     value     =     "both "> Both     Event     and     Task </option>    
              <option     value     =     "event "> Event </option>    
              <option     value     =     "task "> Task </option>    
</select>    
 
<input     type= "submit "     value= "submit "     name     =     "btn_submit "/>    
<input     type     =     "button "     name= "newsearch "     value= "New     Search "     onClick= "addNewSearch() ">  
</html>

------解决方案--------------------
你新增的位置不对,要放在FORM里才行
<html>
<script type= "text/javascript ">
function addNewSearch()
{
var NewOperator = document.createElement( " <select name= 'Operator '> </select> ");
NewOperator.length = 3;
NewOperator.options[0] = new Option( 'And ', 'and ');
NewOperator.options[1] = new Option( 'Or ', 'or ');
NewOperator.options[2] = new Option( 'Not ', 'not ');
document.getElementById( "div1 ").insertBefore(NewOperator);
}
</script>

<form name= "Search " action= "FilterResult.php " method= "post ">

<select name= "SearchWhat " onchange = "se