jquery焦点回到相同的输入字段,错误不适用于所有浏览器
我有一个包含多个字段的表单,其中包含动态创建的字段和一些预定义的字段。 其中一个字段使用jquery timepicker插件,由http://jonthornton.github.io/jquery-timepicker/提供
现在我的问题是我正在对该字段进行快速实时validation,因为它在焦点上模糊不清,如果validation失败,则显示错误并将焦点恢复到该字段。 现在我的代码在chrome上工作,但在IE 11和Firefox上,(inputfiled).focus()没有被触发,因此焦点不会返回到字段。
这是我的代码小提琴http://jsfiddle.net/8cL42bcy/6/
以下是相同的代码片段…
$(document).ready(function() { // this is used just to highlight where the current focus is on... $("input").focus(function() { $(this).css('background-color', 'rgba(240, 116, 116, 0.69)'); }); $("input").blur(function() { $(this).css('background-color', '#fff'); }); // timepicker plugin courtesy of-- http://jonthornton.github.io/jquery-timepicker/ $('#presentationTime').timepicker({ 'scrollDefault': '08:30 AM', 'disableTimeRanges': [ ['8:01pm', '11:59pm'], ['12am', '7:59am'] ], 'timeFormat': 'h:i A', 'selectOnBlur': true }).on('timeFormatError', function() { if ($('#presentationTime').val() != null || $('#presentationTime').val() != "") { $("#errormsg").css("display", "inline").fadeOut(4000); } $('#presentationTime').val(""); $('#presentationTime').focus(); }).on('timeRangeError', function() { if ($('#presentationTime').val() != null || $('#presentationTime').val() != "") { $("#errormsg").css("display", "inline").fadeOut(4000); } $('#presentationTime').val(""); $('#presentationTime').focus(); }); // end timepicker function $("#field").blur(function() { if ($('#field').val() != "12345") { $("#field").css('outline', 'green dotted thick'); $('#field').val(""); $('#field').focus(); } }); });
#errormsg { display: none; margin-left: 15px; color: red; } .ui-timepicker-list li.ui-timepicker-disabled { /*display: none;*/ font-weight: normal; font-size: 12px; }
Field 1:: Field 2::
time slot:: Valid Time required!
Filed 4:: Field 5::
现在在Chrome中,行为如下……
选项卡通过字段时间段并输入乱码和制表符或焦点,它会显示一条消息,焦点将返回到时间段字段。 与field4输入字段相同。 如果输入不是“12345”,则它绘制轮廓并将焦点带回field4输入字段。
在FF和IE中,行为是在无效输入时,焦点返回到相同的输入字段,它转到下一个字段。
我哪里错了?
在此先感谢您的帮助。
这是Firefox和IE中的实现错误,您可以使用计时器解决它。 这是新的小提琴: http : //jsfiddle.net/8cL42bcy/8/
我添加了setTimeout(function() { $('#field').focus(); }, 50);
在末尾。
它不会以任何方式影响Chrome,因为已经集中注意力。
- 删除记录时找不到任何元素
- jQuery背景位置在FireFox中不起作用?
- 将Jquery和其他JS库添加到Firefox扩展
- 使用firefox w / firebug,如何在浏览器中编写javascript并使用.js文件对象呢?
- 如何让CSS mediaqueries与jQuery $(window).innerWidth()一起使用?
- 仍然有jQuery背景动画的问题 – 在safari和IE中工作,没有别的!
- 记录或记录所有浏览器DOM / JQuery事件
- 使用box-sizing时,jQuery.height()在WebKit和Firefox中的行为有所不同:border-box
- 在javascript中取消&符号的&符号