通过java脚本存储表单变量

时间:2016-09-14 18:52:11

标签: javascript html

您好我通过javascript制作了很少的文本字段。但我不知道如何在提交表单时将它们存储到数据库中。这是代码

<script type="text/javascript">
    function showfield(name){
      if(name=='Other') {
        document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
        document.getElementById('div2').innerHTML='';
      }
      else if(name=="School") {
        document.getElementById('div1').innerHTML='Name of the School : <input type="text" name="school" />'; 
        document.getElementById('div2').innerHTML='Contact Number : <input type="text" name="contact" />'; 
      }
      else {
       document.getElementById('div1').innerHTML='';
       document.getElementById('div2').innerHTML='';
     }
   }
 </script>

<html>
<select name="how" class="subtitle" id="select" onchange="showfield(this.options[this.selectedIndex].value)" style="width: 200px; height:30px;">>
                          <option selected="true" style="display:none;">Please select</option>
                          <option>School</option>
                          <option>Consultant</option>
                          <option>WhatApp</option>
                          <option>Brochure / Poster</option>
                          <option>Internet</option>
                          <option>Other</option>
                        </select></td>
</html>

在上面的html中,当&#39; School&#39;或者&#39;其他&#39;选择该选项后,它将显示来自javascript的更多文本字段。但我无法存储该文本字段的值。但所有其他选项都插入到数据库中。请帮忙解决这个问题。

1 个答案:

答案 0 :(得分:1)

从html字段获取值可以通过多种方式完成,这只是其中之一:

<form onsubmit='onSubmit();'>
<input type="text" id="variableName" />
<input type="submit" value="submit" />
</form>

<script type='text/javascript'>
function onSubmit(){
var val = document.getElementById('variableName').value;
console.log(val);
}
</script>

这种方式可以获取用户在输入字段中输入的内容。

这是你想要实现的吗?希望有所帮助...