为提交按钮添加onclick功能 [英] add onclick function for submit button
问题描述
有没有办法将 onclick
函数添加到< input type =submit>
?当在表单中单击提交按钮时,我想显示名为div2的隐藏< div>
。
Is there a way to add an onclick
function to an <input type="submit">
? I would like to show a hidden <div>
named "div2" when the submit button is clicked inside a form.
推荐答案
请记住,您需要停止单击提交按钮的默认行为,否则将提交表单并在 div <之前加载新页面/ code>永远显示。例如:
Remember that you need to stop the default behavior of clicking the submit button or else the form will be submitted and a new page will load before the div
ever get's displayed. For example:
<script type="text/javascript">
function showDiv() {
document.getElementById('div2').style.display = 'block';
return false;
}
</script>
<div id="div2" style="display: none;">Visible</div>
<form name="test_form" action="#">
<input type="submit" onclick="return showDiv();" value="Submit" />
</form>
使用此代码,表单现在不会提交, div $将显示c $ c>。如果您稍后想要提交表单,则需要更改
showDiv()
以返回 true
,使用其他提交按钮或调用表单的 submit()
方法。
Using this code the form will now not submit and the div
will be shown. If you then want to submit the form later you need to either change showDiv()
to return true
, use another submit button or call the submit()
method of the form.
这篇关于为提交按钮添加onclick功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!