4

Base on this question radio selected

I use this way

<input type="radio" class="fp" value="q" name="fp" checked>
fp = $('input[name=fp]:checked').val();
alert(fp);

To get the value of select radio box. Please see this fiddle

But it is not worked under IE7 and IE8. Always got "Object doesn't support this property or method" . Any suggestions?

Jonas
  • 121,568
  • 97
  • 310
  • 388

6 Answers6

2

You just needed to set your value into a variable, as shown:

var fp = $("input[name='fp']:checked").val();
alert(fp);

Example

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
2

Declare the variable before using it:

http://jsfiddle.net/5cjK7/13/

var fp = $('input[name=fp]:checked').val();

alert(fp);
Esailija
  • 138,174
  • 23
  • 272
  • 326
1

IE can have unexpected behaviour with the pseudo selectors use attr or prop(1.6+)

fp = $("input[name='fp']").filter(function(){return $(this).attr("checked");}).val();
Rafay
  • 30,950
  • 5
  • 68
  • 101
0

In my case I was getting this behavior because the form submission called a function which replaced the form's html and then checked for the checked item:

var sizeSelected = $(this).find("input[name='size']:checked").val();

In hindsight it's strange this works in Chrome, Firefox and Safari, but it does. Only IE/Edge bonks with sizeSelected == null. The solution is to check the form results before clearing/replacing the form's html.

jtbr
  • 1,149
  • 13
  • 13
0

This may seem really simple, but I believe the issue deals with the fact that you are assigning the value to a variable named "fp" which happens to match the name of the form element. Internet Explorer already has a variable in scope named "fp" which is the radio button element itself. Assigning a string value to that variable is not allowed. You can solve this one of two ways:

// Declare a new variable
var fp = $('input[name=fp]:checked').val();
alert(fp);

or ...

// Assign to a new variable in global scope (not recommended)
xy = $('input[name=fp]:checked').val();
alert(xy);
Phil Klein
  • 7,344
  • 3
  • 30
  • 33
0

You are not closing your inout element and the attr. checked should have a value like;

<input type="radio" class="fp" value="q" name="fp" checked="checked" />

Also; - You need to declare the variable before you use it. - The jQuery selector should contain quotes. - The selector returns a collection of elements so you should extract the first hit, perhaps by using first().

var fp = $('input[name="fp"]:checked').first().val();
Stefan
  • 5,644
  • 4
  • 24
  • 31