JQuery gets switch id by checking value

I have only a read form that I want to fill with the values โ€‹โ€‹returned from the ajax request. What I don't understand is the correct syntax to get the identifier of a specific switch. I know what value exists in db, but how to get switch id to change switch attribute?

For example, in the parameters of the "Radio" button with the "name" == Criteria1Score ", how can I return the input identifier, where value == Good.

Here is an example of a form structure:

<legend>Criteria1</legend> <label class="radio inline"> Great <input id="Criteria1Score1" class="Score" name="Criteria1Score" type="radio" value="Great"> </label> <label class="radio inline"> Good <input id="Criteria1Score2" class="Score" name="Criteria1Score" type="radio" value="Good"> </label> <label class="radio inline"> Bad <input id="Criteria1Score3" class="Score" name="Criteria1Score" type="radio" value="Bad"> </label> 
+8
source share
6 answers

$('input[type=radio][name=Criteria1Score]:checked').attr('id')

See fiddle example

+30
source

You can scroll through these inputs to do what you need:

 $("input[name='Criteria1Score']").each(function() { if (this.value == "Good") { //do something with this input } }); 
0
source
 $("input[value='Good']").attr("id"); 

If you want to select an input by its value

0
source

You can try

 var selected_Id = $('input[name="Criteria1Score"]:selected').attr('id'); 

EDIT: Sorry, did not see the second part. If you want to check value == OK, you can do

 if ($('input[name="Criteria1Score"]:selected').val() == 'Good') { //do something } 
0
source

I got it using checked.

 var selected_Id = $('input[name="Criteria1Score"]:checked').attr('id'); if ($('input[name="Criteria1Score"]:checked').val() == 'Good') { //do something } 
0
source

Jquery gets the switch id by checking the value ,

 <legend>You are ready for</legend> <label class="radio inline"> Success <input id="success" class="win" name="successvalue" type="radio" value="Great"/> </label> <label class="radio inline"> Big Success <input id="bigsuccess" class="win" name="successvalue" type="radio" value="Good"/> </label> <br/> <input type="button" onclick="alert($('input[name=successvalue]:checked').attr('id'))" value="click me too" /> 

Watch the violin

Worked like code to get the id value on the switch

-1
source

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


All Articles