我有一个表单,带有提交按钮:
<% form_for(@personalentry) do |f| %>
    <%= f.submit 'Create' %>
<% end %>
HTML输出
<form action="/personalentries" class="new_personalentry" id="new_personalentry" method="post">
    <input id="personalentry_submit" name="commit" type="submit" value="Create" />
</form>
我想添加第二个按钮,以执行与表单提交无关的其他代码.所以我这样做了:
<head>
    <script type="text/javascript">
        function addEndDateCalendar(){
            alert("test");
        }
    </script>
</head>
<body>
    <form action="/personalentries" class="new_personalentry" id="new_personalentry" method="post">
        <button name="addEndDateButton" onClick="addEndDateCalendar()">End date?</button>
        <input id="personalentry_submit" name="commit" type="submit" value="Create" />
    </form>
</body>
当我单击按钮时,警报会出现,但随后会运行提交代码.在运行自己的代码后,如何让第二个按钮不运行提交代码?谢谢阅读.
您可以尝试使用type="button":
<input type="button" name="addEndDateButton" onClick="addEndDateCalendar()" value="End date?" />