2017-06-01 70 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> 
相关问题