Filtering switches in jquery, comparing values

I have the following code. Trying to preset a switch based on the value of the incoming array. It will be 1-3

<input name="rating1" type="radio" value="1" /> <input name="rating1" type="radio" value="2" /> <input name="rating1" type="radio" value="3" /> $(document).ready(function() { //Setting up the radio buttons var $radios = $('input:radio[name=rating1]'); //ratingAnswer is the value I need to select in the radio group. Value=2 var ratingAnswer=answersArray[oneMap].RATING; if($radios.is(':checked') === false) { $radios.filter('[value=ratingAnswer]').attr('checked', true); } 

This does not return me anything, as it searches the string ratingAnswer as the value, not the value of the ratingAnswer variable. I tried value = '+ ratingAnswer +', but I don't like this at all. How can I insert a variable into this value range. Thanks in advance.

+4
source share
2 answers
 $radios.filter('[value="' + ratingAnswer + '"]').attr('checked', true); 

If this still does not work, look carefully at the value of ratingAnswer to see what you expect.

Example: http://jsfiddle.net/6zAN7/1/

+5
source

You should change this line:

  $radios.filter('[value=ratingAnswer]').attr('checked', true); 

for this:

  $radios.filter('[value='+ratingAnswer+']').attr('checked', true); 
+2
source

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


All Articles