Родитель 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>
|
<!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>
может братский, а не родительский? |
<!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 можно обращаться напрямую |
Не важно)
А если у меня проблема стоит в том, что элемент inTest - массив. <div class=test> <input id='inTest' > <input id='inTest' > <button type='button' id='add'>Отправить</button> </div> И нужно получить все val |
<!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>
|
id должен быть уникальный.
так делать не хорошо! |
Да, но как это сделать с родителем?
$('#test').on('click', 'add', function(){
$(this).siblings('#inTest').each(function(){
$(this).attr('disabled', false);
});
});
Так не работает. |
что вы хотите сделать?
|
Впрочем у меня есть форма которая добавляет путем Jquery новые записи.
Они все имеют id inTest. нужно, чтобы каждая новая добавленная запись, при отправки формы убирала disabled с input. |
| Часовой пояс GMT +3, время: 12:22. |