最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make .focus() work on a radio button array? - Stack Overflow

programmeradmin0浏览0评论

I am trying to get the .focus() working in IE, it works in chrome etc. My form is called:

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

my radio buttons:

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

in my javascript I am trying to call using this line:

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

As I said, it works in chrome, firefox blah blah blah but in IE 8 I am getting nada, zip and I don't know why, nor can I find a satisfactory answer, is there a way around it?

I am trying to get the .focus() working in IE, it works in chrome etc. My form is called:

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

my radio buttons:

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

in my javascript I am trying to call using this line:

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

As I said, it works in chrome, firefox blah blah blah but in IE 8 I am getting nada, zip and I don't know why, nor can I find a satisfactory answer, is there a way around it?

Share Improve this question edited Apr 14, 2010 at 3:57 Michael Petrotta 61k27 gold badges152 silver badges181 bronze badges asked Apr 14, 2010 at 1:54 flavour404flavour404 6,32230 gold badges106 silver badges140 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2
<input type="radio" name="fb_mentype" value="Comment" />Comment
<input type="radio" name="fb_mentype" value="Complaint" />Complaint
<input type="radio" name="fb_mentype" value="Request" />Request

Simple, look at your radio button as an array, you can focus any radio button of the array by pointing to the right index, see below.

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

that way the "Comment" radio button will be focused...

Happy coding!!

Are you calling focus when the page just loads (e.g. body's onload)?

This thread might help - it may get called in your code before DOM finished loading

Also, this page has a good test for focus() behavior: http://hardlikesoftware./projects/IE8FocusTest.html

This might be that IE does not understand the .[0].focus() syntax. You may want to try something like this:

document.forms["feedbackform"].elements["fb_mentype"][0].focus();
document.forms["feedbackform"].elements["fb_mentype"].focus(); 
发布评论

评论列表(0)

  1. 暂无评论