Javascript / HTML转移表单控件提交

时间:2014-08-06 01:51:21

标签: javascript html forms submit getelementbyid

我一直在尝试创建一个包含5个文本框,1个下拉列表和一个文本区域的表单。我的意图很简单;从下拉列表中选择一个选项(除了第一个,即#34; - 选择 - "),在文本框和文本区域中输入内容。点击提交后,所有上述输入的信息都应附加到硬编码到每个下拉选项中的特定链接。

我的挑战是,当我点击提交时,控件不会传递给新表单,即打开所需页面,但不会输入表单字段。我已经交叉验证并确认ID是正确的。我猜测我在验证中犯了一个错误。请指出我的错误..

Javascript代码

<script type="text/javascript">

function dpdown(form)
{
    var a="&name="+document.getElementById('name').value;  //textbox
    var b="&job_id="+document.getElementById('job_id').value;//textbox
    var c="&major"+document.getElementById("major').value;//textbox
    var d="&native="+document.getElementById('native').value;//textbox
    var e="&age="+document.getElementById('age').value;//textbox
    var f="&details="+encodeURIComponent(document.getElementById('details').value);//textarea
    var i, counter = 0;
    var ddl = document.getElementById("jobs");//dropdown
    var selectedValue = ddl.options[ddl.selectedIndex].value;
    for(var i=0; i < obj.options.length; i++)
         {   
              if(obj.options[i].selected) // to check if the person has chosen a suitable degree
                    if (selectedValue == "selectdegree")
                     {
                       alert("Please select your correct degree");
                     } 
                     else
                     {
                       ++counter;
                       window.open('https://'+form.elements[i].value+a+b+c+d+e+f,'_blank'); // when the submit button is clicked, all the above fields should get appended to a preset link which has been hardcoded for each degree.
                     }
          }
}

function cleartext()// to clear the form (works correctly)
{
 document.form.details.value='';
}

HTML代码

    <form onsubmit="dpdown(this); return false;"> // at the beginning of the form
'
'
'
'
 <input type="submit" value=" Create form" /> // not working
 <input type='reset' value='Clear Ticket' name='reset' onclick="return resetForm(this.form);cleartext();"> // works well

1 个答案:

答案 0 :(得分:0)

请检查一下。

<html>
<head>
    <script language="javascript">
        function dpdown(){
            var a="&name="+document.getElementById('name').value;  //textbox
            var b="&job_id="+document.getElementById('job_id').value;//textbox
            var c="&major"+document.getElementById('major').value;//textbox
            var d="&native="+document.getElementById('native').value;//textbox
            var e="&age="+document.getElementById('age').value;//textbox
            var f="&details="+encodeURIComponent(document.getElementById('details').value);//textarea
            var i, counter = 0;
            var ddl = document.getElementById("jobs");//dropdown
            var selectedValue = ddl.options[ddl.selectedIndex].value;
            for(var i=0; i < ddl.length; i++)
                 {   
                      if(ddl.options[i].selected) // to check if the person has chosen a suitable degree
                            if (selectedValue == "selectdegree")
                             {
                               alert("Please select your correct degree");
                             } 
                             else
                             {
                               ++counter;
                               value=ddl.options[i].value;
                               window.open('https://'+value+a+b+c+d+e+f,'_blank'); // when the submit button is clicked, all the above fields should get appended to a preset link which has been hardcoded for each degree.
                             }
                  }
        }

        function cleartext()
        {
         document.form.details.value='';
        }
    </script>
</head>
<body>
    <form> 
        <input type="text" id="name"/>
        <input type="text" id="job_id"/>
        <input type="text" id="major"/>
        <input type="text" id="native"/>
        <input type="text" id="age"/>
        <input type="textarea" id="details"/>
        <select id="jobs" >
        <option value="www.google.com">google</option>
        <option value="www.yahoo.com">yahoo</option>
        <option value="www.stackoverflow.com">stackoverflow</option>        
        </select>
        <input type="button" value=" Create form" onclick="dpdown();"/> 
        <input type='reset' value='Clear Ticket' name='reset' onclick="return resetForm(this.form);cleartext();">
    </form>
</body>
</html>