JQuery RadioButton Index

How to get RadioButton verification index using jQuery? I need to read it and save it in cookies to load state later using code

$('input[name="compression"]')[{Reading from cookies code}].checked = true; 1<input name="compression" type="radio" value="1" checked="checked" /> 2<input name="compression" type="radio" value="2" /> 3<input name="compression" type="radio" value="3" /> 

Relations Thomas

+6
jquery radio-button indexing
source share
1 answer

This returns an index based on the zero value of the selected radio book

 $('input[name=compression]:checked').index() 

In the case described in the use of the comment (which is usually the correct syntax to use)

 $('input[name=porient]:checked').index('input[name=porient]') 

If you want to know why the first of them gives incorrect results, you should read . index () documentation

If no argument is passed to .index (), the return value is an integer indicating the position of the first element in the jQuery object relative to its sibling elements.

In the sample of the question, there were no β€œother” brothers and sisters other than your ham radio. So it works.

In the example from the comment, the <br /> tag is a brother of radio objects and therefore is located at index 1. That's why you think index() giving the wrong position

+7
source share

All Articles