2014-02-07 40 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方法更好(更安全),特別是如果您發送密碼。