2014-02-07 44 views
1

我负责的一个可怕的问题,我开发了集多领域的形式更多的字段(正常使用DIV) ,当我测试提交操作我在URL中的一个输入不填充了,也隐藏在 的标记中,如下图所示。HTML表单submiting比预期

http://127.0.0.1:8000/exform.html?user=test&pwd=QWERT1234&user_reg=  

这里是表单代码:

<form id="exform"> 
      <div class="fields" id="login"> 
       <div class="txt"> 
        <label for="user"></label> 
        <input id="user" type="text" name="user"/> 
       </div> 

       <div class="txt"> 
        <label for="pwd"</label> 
        <input id="pwd" type="password" name="pwd" /> 
       </div> 
        <input type="submit" value="test" id="test"/> 
      </div> 

      <div class="fields" id="register"> 

       <div class="txt"> 
        <label for="user_reg"></label> 
        <input id="user_reg" name="user_reg" type="text"/> 
       </div> 

       <div class="txt"> 
        <label for="pwd2"></label> 
        <input id="pwd2" type="password" /> 
       </div> 

       <div class="txt"> 
        <label for="pwdc"></label> 
        <input id="pwdc" type="password"/> 

       </div> 

       <div class="buttons"> 
        <input type="submit" value="OK" id="ok"/> 
       </div> 
      </div> 
</form> 

奇怪的是,第二场集不在屏幕可用,因为在CSS 有只有一个规则显示第一组与类“字段”

/*Hide all except first div with class div*/ 
#exform .fields:not(:first-of-type) { 
    display: none; 
} 

所以我真的想知道为什么窗体提交字段超出范围。

例如,如果使用第二组字段集,当按钮与值确定提交被点击所产生的结果是类似的。在URL中,只有user_reg场参数表现出充满了对第一组两个另一场无值:

http://127.0.0.1:8000/exform.html?user=&pwd=&user_reg=test 

下面的代码是提交测试:

$(function() { 

    $('#test').click(function() { 
     $('#exform').submit(function(event) { 
      console.log("form Submited:" + document.forms['exform'] + "test"); 
     }); 
    }); 
    $('#ok').click(function() { 

     $('#exform').submit(function(event) { 
      console.log("form Submited:" + document.forms['exform'] + "ok"); 
     }); 
    }); 
}); 

没关系我得到了相同的URL导致

http://127.0.0.1:8000/exform.html?user=test&pwd=QWERT1234&user_reg= 

http://127.0.0.1:8000/exform.html?user=&pwd=&user_reg=test 

相反,我收到:

// on #test click 
http://127.0.0.1:8000/exform.html?user=test&pwd=QWERT1234  

// on #ok click 
http://127.0.0.1:8000/exform.html?user_reg=test&pwd2=QWERT1234&pwdc=QWERT123 

为提交后得到的参数,我不能检索的URL pwd2和pwdc字段中的值。

这让我发疯。

回答

1

如果不指定形式的method方法,则默认为GET同时提交。这就是在您的网址中查看所有表单元素的原因。 试试这个:

<form id="exform" method="post"> 
<!-- form contents --> 

详见here

1

当您提交表单时,您将所有的输入字段提交到表单中。

即使你用css隐藏某些东西,它仍然存在于html中。

当您处理的形式,你可以添加一个隐藏字段“输入类型=”隐藏“”,给该字段,告诉你要在女巫的情况下处理您的脚本巫域的值。

而且我也认为post方法更好(更安全),特别是如果您发送密码。