Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   Родитель Jquery (https://javascript.ru/forum/events/70782-roditel-jquery.html)

Fiks 02.10.2017 19:40

Родитель Jquery
 
Как получить значение родителя:

<div class=test>
  <input id='inTest'>
  <button type='button' id='add'>Отправить</button>
</div>


<script>
  $('#add').on('click', function () {
        alert($('#inTest').parents('.test').val());
    });</script>

j0hnik 02.10.2017 19:54

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div class=test>
		<input id='inTest'>
		<button type='button' id='add'>Отправить</button>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script>
		$('#add').on('click', function(){
			alert($(this).siblings('#inTest').val());
		});
	</script>
</body>
</html>


может братский, а не родительский?

j0hnik 02.10.2017 20:02

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div class=test>
		<input id='inTest'>
		<button type='button' id='add'>Отправить</button>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script>
		$('#add').on('click', function(){
			alert($('#inTest').val());
		});
	</script>
</body>
</html>


и вообще если есть id можно обращаться напрямую

Fiks 02.10.2017 20:03

Не важно)

А если у меня проблема стоит в том, что элемент inTest - массив.

<div class=test>
  <input id='inTest' >
  <input id='inTest' >
  <button type='button' id='add'>Отправить</button>
</div>


И нужно получить все val

j0hnik 02.10.2017 20:06

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div class=test>
		<input id='inTest' name='val[]'>
		<input id='inTest' name='val[]'>
		<button type='button' id='add'>Отправить</button>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script>
		$('#add').on('click', function(){
			$(this).siblings('input').each(function(){
				alert(this.value);
			});
		});
	</script>
</body>
</html>

j0hnik 02.10.2017 20:07

id должен быть уникальный.
так делать не хорошо!

Fiks 02.10.2017 21:16

Да, но как это сделать с родителем?
$('#test').on('click', 'add', function(){
        $(this).siblings('#inTest').each(function(){
            $(this).attr('disabled', false);
        });
    });

Так не работает.

j0hnik 02.10.2017 22:55

что вы хотите сделать?

Fiks 02.10.2017 22:57

Впрочем у меня есть форма которая добавляет путем Jquery новые записи.
Они все имеют id inTest. нужно, чтобы каждая новая добавленная запись, при отправки формы убирала disabled с input.


Часовой пояс GMT +3, время: 03:59.