1

$('input:not(:first)').click(function() {
  var element1 = $('span').html();
  console.log(element1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#">
  <input id="1" type="radio" name="example"><label for="1">number one <span>50.1</span></label>
  <input id="2" type="radio" name="example"><label for="2">number two <span>100.5</span></label>
  <input id="3" type="radio" name="example"><label for="3">number three <span>200.9</span></label>
</form>

Предполагаю, что это все должно быть связано с this

2
  • Добрый вечер. Да, используйте this
    – user216615
    25 фев 2018 в 16:20
  • @ slo_nik вопрос - как?
    – Proshka
    25 фев 2018 в 16:20

1 ответ 1

4

В вашем случае HTML идет после инпута, поэтому, сначала получим следующий после инпута элемент (метод next()), потом достанем его деток (метод children()), а потом достанем HTML:

$('input:not(:first)').click(function() {
  var element1 = $(this).next().children().html();
  console.log(element1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#">
  <input id="1" type="radio" name="example"><label for="1">number one <span>50.1</span></label>
  <input id="2" type="radio" name="example"><label for="2">number two <span>100.5</span></label>
  <input id="3" type="radio" name="example"><label for="3">number three <span>200.9</span></label>
</form>

3
  • нет, нужно содержимое <span>
    – Proshka
    25 фев 2018 в 16:25
  • ну так сразу и надо было писать )))) внес правку в ответ, смотрите 25 фев 2018 в 16:26
  • Спасибо за помощь! Одним словом, в таких случаях нужно применять функции перемещения по DOM
    – Proshka
    25 фев 2018 в 16:29

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.