我有一个表单,带有提交按钮:
<% 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>
单击按钮时,警报会出现,但随后会运行提交代码。在运行自己的代码后,如何让第二个按钮不运行提交代码?谢谢你的阅读。
答案 0 :(得分:5)
您可以尝试使用type="button"
代替:
<input type="button" name="addEndDateButton" onClick="addEndDateCalendar()" value="End date?" />
答案 1 :(得分:0)
如果您的函数addEndDateCalendar()
返回false
,则不应提交提交。