2017-06-01 15 views
1

我有多個textboxesbuttons一個form。我想用一些按鈕來清除textbox。但是,當我點擊按鈕Clear text表格發佈。在<form>的所有按鈕都張貼

有人知道我該如何解決這個問題嗎?

這裏是我的HTML:

<form name="reaction" method="post" action="./send.php"> 
    <input type="text" name="firstname"> 
    <button class="btn btn-default" onclick="ClearText1();">Clear text</button><br /> 

    <input type="text" name="lastname"> 
    <button class="btn btn-default" onclick="ClearText2();">Clear text</button><br /> 

    <button class="btn btn-block btn-success">Send</button> 
</form> 
+0

使用'<按鈕類型=「提交」'用於其他按鈕 – XYZ

+0

@XYZ那是溶液提交按鈕和'<按鈕類型=「按鈕」'。謝謝 – John

回答

1

使用<button type="submit"的提交按鈕,0​​其他button.You可以閱讀更多關於按鈕在這裏。

MDN

按鈕的類型屬性。可能的值是:

  1. 提交:按鈕的表單數據提交到服務器。如果未指定屬性,或者屬性動態更改爲空值或無效值,則這是默認值。

  2. 重置:該按鈕將所有控件重置爲其初始值。

  3. 按鈕該按鈕沒有默認行爲。它可以將客戶端腳本與元素的事件相關聯,這些事件在事件發生時觸發。

  4. 菜單:該按鈕打開通過其指定元素定義的彈出菜單。

<form name="reaction" method="post" action="./send.php"> 
 
    <input type="text" name="firstname"> 
 
    <button type="button" class="btn btn-default" onclick="ClearText1();">Clear text</button><br /> 
 

 
    <input type="text" name="lastname"> 
 
    <button type="button" class="btn btn-default" onclick="ClearText2();">Clear text</button><br /> 
 

 
    <button type="submit" class="btn btn-block btn-success">Send</button> 
 
</form>

0

使用event.preventDefault();以防止默認提交此按鈕的行爲。

<script> 
     function ClearText2(event){ 
      // your code.... 
      event.preventDefault(); 
     } 
    </script>