Javascript:事件处理/内容更新/提交
这是我的简单HTML表单(简称):Javascript:事件处理/内容更新/提交,javascript,event-handling,form-submit,Javascript,Event Handling,Form Submit,这是我的简单HTML表单(简称): <form name="formular" action="./import.php" method="post" onsubmit="buttonClicked();"> <input name="notes" onchange="notesChanged();" /> <input name="info" onchange="infoChanged();" /> <input name="d
<form name="formular" action="./import.php" method="post" onsubmit="buttonClicked();">
<input name="notes" onchange="notesChanged();" />
<input name="info" onchange="infoChanged();" />
<input name="doit" class="button" type="submit" value="validate" />
</form>
更改这两个字段时,按钮标签可能会更改为“提交”、“提交不同”或返回到“验证”。
当编辑并通过“tab”留下一个字段时,这非常有效:用户以特殊方式显示,表单被提交
我的问题:
<form name="formular" action="./import.php" method="post" onsubmit="buttonClicked();">
<input name="notes" onchange="notesChanged();" />
<input name="info" onchange="infoChanged();" />
<input name="doit" class="button" type="submit" value="validate" />
</form>
当一个字段被更改并且不是通过“tab”离开,而是直接点击按钮离开时,标签也可能被更改,在这种情况下,不应提交表单
我不知道如何处理这种情况,因为所有字段事件都是在任何按钮事件之前处理的。您可以使用jquery添加模糊事件。当输入字段失去焦点时触发:
<script type="text/javascript">
$(function () {
$('#notes').blur(function () {
notesChanged();
});
$('#info').blur(function () {
infoChanged();
});
});
</script>
<form name="formular" action="./import.php" method="post" onsubmit="buttonClicked();">
<input id="notes"/>
<input id="info"/>
<input name="doit" class="button" type="submit" value="validate" />
</form>
$(函数(){
$('#notes').blur(函数(){
notesChanged();
});
$('#info').blur(函数(){
infoChanged();
});
});
在较新的浏览器中,您可以使用输入事件。此功能类似于change
事件,但不要求元素在触发前失去焦点(由于按下tab键或用户单击页面上的其他位置)。包含有关浏览器支持的信息
这将确保在用户更改其他两个输入时更新按钮的文本,从而在用户单击按钮本身之前更新按钮。在较新的浏览器中,您可以使用input
事件而不是change
事件,因为这不要求元素在触发之前失去焦点。不过,这取决于notesChanged
和infoChanged
做了什么。这对我来说是新的,而且是我做的。如果你作为答案发布,我将检查是否接受。Thxonblur也在onclick/onsubmit之前处理(如onchange)