I need to submit the content of a form when I press the Enter key, but only if the form has no error message. I built up the following function:
$(targetFormID).submit(function (e) {
var mess = error_m(targetDiv);
if (e.keyCode == 13 && mess.length > 0) {
e.preventDefault();
e.stopPropagation();
}
if (mess.length == 0 && e.keyCode == 13) $(targetFormID).submit();
});
在此函数中,mess 变量获取函数返回的错误消息error_m
,其余的是简单的代码条件,但它不起作用。
需要一些帮助!
Submitting the form when the Enter key is pressed is default browser behaviour. Don't mess with it. Just validate the form in the submit
event.
$(targetFormID).submit(function (e) {
var mess = error_m(targetDiv);
if (mess.length > 0) {
e.preventDefault();
}
});
另一个可能的问题:什么是targetFormID
?如果它实际上是一个包含元素 ID 的字符串,那么您需要
$("#" + targetFormID).submit(/* Same function as above */);
如果它是对表单元素的引用那么$(targetFormID)
很好,但你的变量命名有误导性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)