简体   繁体   English

带有onchange()回调的表单输入在提交时更新

[英]Form inputs with onchange() callbacks update on submit

I have a form with some input fields and all those fields have onchange triggered callbacks (which can be different depending on the field). 我有一个带有一些输入字段的表单,所有这些字段都有onchange触发的回调(根据字段的不同,可能有所不同)。

My problem is that when the form is submitted the focus is still on the last modified field and the onchange callback is not called for this field, which can lead to submit wrong values. 我的问题是,提交表单时,焦点仍然集中在最后修改的字段上,并且该字段未调用onchange回调,这可能导致提交错误的值。

I am looking for a way to force the triggering on the onchange event for the last field modified but I can't determine which element it is from my submit function. 我正在寻找一种方法来强制对上一次修改的字段的onchange事件进行触发,但是我无法从我的submit函数中确定它是哪个元素。

How can I achieve this ? 我该如何实现?

Try this 尝试这个

$focused_element = $(':focus');
if($focused_element.is('input')){
  $focused_element.trigger('change');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM