Get the value of the next input

<form method="post" id="formFinish2"> <input type="checkbox" name="finish" /> <input type="hidden" name="id" class="finishId" value="2" /> </form> <script type="text/javascript"> $(":checkbox[name='finish']").click(function() { id = $(this).closest('input.finishId').val(); alert(id); $("#formFinish".id).submit(); }); </script> 

Notifies me undefined . I would like it to warn me 2 , which comes from input that has a finishId class, next to the checkbox selected.

+4
source share
2 answers

This is because closest searches only the DOM up . In other words, closest starts the search with the father element, then the grandfather element , etc.

Instead, use siblings .

 $(this).siblings('input.finishId').val(); 
+5
source

Besides the function of siblings, you can also use the following function. See http://api.jquery.com/next/

+2
source

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


All Articles