javascript 焦点功能未按预期工作

问题描述 投票:0回答:4

我有一个 jsp/html 页面。该页面是从两个不同的链接访问的。 它是 Menu1-->demo.html 页面和 Menu2-->demo.html 页面(在我的例子中是 jsp) 在 demo.html 页面中,我有一个默认禁用的文本框。

<input class="mytextinputtextField" type="text" id="countryOfOrigin"
readonly style="background-color:#DCDCDC"   />

现在,当从 Menu2 访问 demo.html 页面时,我使用隐藏参数进行检查,并再次启用文本框并将焦点设置在文本框上。

if(document.getElementById("submenu_name").value=="PM")
{
   document.getElementById("countryOfOrigin").focus();
}

该字段已启用并且光标可见,但如果我键入文本,则不会在文本框中键入文本。 我需要单击文本框来输入文本。 我认为这是因为禁用并再次启用字段。 请任何人帮助我能够在 javascript 焦点事件的文本框中输入内容。 我没有使用 JQuery,浏览器是 I.E 6

javascript html css
4个回答
0
投票

试试这个:

document.getElementById("countryOfOrigin").removeAttribute('readOnly');

jQuery 版本:

jQuery('#countryOfOrigin').removeAttr('readOnly');

0
投票

您是否尝试过删除该属性?

document.getElementById("countryOfOrigin").Attributes.Remove("readonly");

document.getElementById("countryOfOrigin").removeAttribute("readonly");

0
投票

感谢您的帮助。 我已经找到解决方案或者可以说解决方法。 我粘贴的代码可能对其他人有用

if(document.getElementById("submenu_name").value=="PM")
{
 document.getElementById("countryOfOrigin").focus();
 document.getElementById("countryOfOrigin").select();

}

    

-1
投票

这里有一个非官方的解决方案,我们必须使用 setTimeout() 函数来延迟 focus() 的执行时间。

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);

参见 http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

© www.soinside.com 2019 - 2024. All rights reserved.