How can I get which radio is selected through jQuery in IE7 and IE8?

Based on this question, the selected radio station

I use this method

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

Get the value of the selection field. See This Fiddle

But it does not work under IE7 and IE8. Always got "The object does not support this property or method." Any suggestions?

+4
source share
6 answers

You just needed to set the value to a variable as shown below:

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

Example

+2
source

Declare a variable before using it:

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

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

IE may have unexpected behavior with pseudo selectors using attr or prop (1.6 +)

 fp = $("input[name='fp']").filter(function(){return $(this).attr("checked");}).val(); 
+1
source

This may seem very simple, but I think the problem is with the fact that you are assigning a value to a variable named "fp" that matches the name of the form element. Internet Explorer already has a variable in an area named "fp", which itself is a switch item. Assigning a string value to this 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); 
0
source

You do not close your inout element and attr. checked should have a type value:

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

Also; - You must declare a variable before using it. - The jQuery selector must contain quotation marks. - The selector returns a collection of elements, so you should retrieve the first hit, possibly using first() .

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

In my case, I got this behavior because the form submission called a function that replaced the html form and then checked for the marked element:

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

In retrospect, it is strange that this works in Chrome, Firefox and Safari, but it is. Only IE / Edge bonks with sizeSelected == null . The solution is to check the results of the form before cleaning / replacing the html form.

0
source

Source: https://habr.com/ru/post/1385552/


All Articles