如何使.focus()在单选按钮数组上工作?

时间:2010-04-14 01:54:18

标签: javascript asp-classic

我试图让.focus()在IE中工作,它在chrome等工作。我的表单被称为:

<form name="feedbackform" action="feedback.asp" target="_self" onsubmit="return 
        validate_txt(this)" method="post" style="margin: 0;">

我的单选按钮:

<input type="radio" name="fb_commentype" value="Comment" />Comment
<input type="radio" name="fb_commentype" value="Complaint" />Complaint
<input type="radio" name="fb_commentype" value="Request" />Request

在我的javascript中我试图使用此行调用:

document.forms["feedbackform"].elements["fb_commentype"][0].focus();

正如我所说,它适用于chrome,firefox等等等等但是在IE 8中我得到了nada,zip并且我不知道为什么,我也找不到满意的答案,有没有办法绕过它? / p>

3 个答案:

答案 0 :(得分:2)

<input type="radio" name="fb_commentype" value="Comment" />Comment
<input type="radio" name="fb_commentype" value="Complaint" />Complaint
<input type="radio" name="fb_commentype" value="Request" />Request

简单,将您的单选按钮看作一个数组,您可以通过指向正确的索引来聚焦数组的任何单选按钮,请参见下文。

document.getElementsByName('fb_commentype')[0].focus();

这样“评论”单选按钮就会聚焦......

快乐的编码!!

答案 1 :(得分:0)

您是否在页面加载时调用焦点(例如正文的onload)?

This thread might help - 在DOM加载完

之前,可能会在代码中调用它

此外,此页面对focus()行为有一个很好的测试: http://hardlikesoftware.com/projects/IE8FocusTest.html

答案 2 :(得分:0)

这可能是因为IE不理解。[0] .focus()语法。你可能想尝试这样的事情:

document.forms["feedbackform"].elements["fb_commentype"][0].focus();
document.forms["feedbackform"].elements["fb_commentype"].focus();