Mat*_*rts 84 html javascript forms
我正在寻找创建没有提交按钮的HTML表单的最佳方法.这本身很容易,但我还需要在完成类似提交的事情时停止表单重新加载(例如,Enter在文本字段中点击).
Dut*_*432 183
您希望包含action="javascript:void(0);"在表单中以防止页面重新加载和维护HTML标准.
K P*_*ime 43
向表单添加一个onsubmit处理程序(通过普通的js或jquery $().submit(fn)),并返回false,除非满足您的特定条件.
除非您不希望表单提交,否则 - 在这种情况下,为什么不在表单元素上省略'action'属性?
只需将此事件添加到文本字段即可.它会阻止按Enter键提交,您可以根据需要随意添加提交按钮或调用form.submit():
onKeyPress="if (event.which == 13) return false;"
Run Code Online (Sandbox Code Playgroud)
例如:
<input id="txt" type="text" onKeyPress="if (event.which == 13) return false;"></input>
Run Code Online (Sandbox Code Playgroud)
一个主意:
<form method="POST" action="javascript:void(0);" onSubmit="CheckPassword()">
<input id="pwset" type="text" size="20" name='pwuser'><br><br>
<button type="button" onclick="CheckPassword()">Next</button>
</form>
Run Code Online (Sandbox Code Playgroud)
和
<script type="text/javascript">
$("#pwset").focus();
function CheckPassword()
{
inputtxt = $("#pwset").val();
//and now your code
$("#div1").load("next.php #div2");
return false;
}
</script>
Run Code Online (Sandbox Code Playgroud)
当你在表单中按Enter键时,表单的自然行为将被提交,为了阻止这种不自然的行为,你必须使用jquery阻止它提交(默认行为):
$("#yourFormId").on("submit",function(event){event.preventDefault()})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99699 次 |
| 最近记录: |