Jquery查找下一个和上一个div行id

标签 jquery

I want to find next div row id My Div Structure id

<div class="section row" id="row_1">
                    <div class="col-md-2"> <h6 class="fw400">Date</h6>
                        <label for="date_1" class="field prepend-icon">
                            <input type="text" id="date_1" name="date_1" class="gui-input" placeholder="Date" value="2017-02-18" readonly>
                            <label class="field-icon"><i class="fa fa-calendar"></i>
                            </label>
                        </label>
                    </div>
</div>
<div class="section row" id="row_2">
                    <div class="col-md-2"> <h6 class="fw400">Date</h6>
                        <label for="date_2" class="field prepend-icon">
                            <input type="text" id="date_2" name="date_2" class="gui-input" placeholder="Date" value="2017-02-18" readonly>
                            <label class="field-icon"><i class="fa fa-calendar"></i>
                            </label>
                        </label>
                    </div>
</div>

When I clicked on first row of Input box(date_1) then that time i want to get next row div id which is "row_2" . I tried but i dont know how to get this. Can you just tell me how to do this??

最佳答案

您可以使用next()prev()方法获取相邻 sibling 。稍后使用 attr() 获取 id 属性方法。

$('.row').click(function() {
  console.log('Prev : ' + ($(this).prev().attr('id') || 'Not found'));
  console.log('Next : ' + ($(this).next().attr('id') || 'Not found'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="section row" id="row_1">
  <div class="col-md-2">
    <h6 class="fw400">Date</h6>
    <label for="date_1" class="field prepend-icon">
                            <input type="text" id="date_1" name="date_1" class="gui-input" placeholder="Date" value="2017-02-18" readonly>
                            <label class="field-icon"><i class="fa fa-calendar"></i>
                            </label>
    </label>
  </div>
</div>
<div class="section row" id="row_2">
  <div class="col-md-2">
    <h6 class="fw400">Date</h6>
    <label for="date_2" class="field prepend-icon">
                            <input type="text" id="date_2" name="date_2" class="gui-input" placeholder="Date" value="2017-02-18" readonly>
                            <label class="field-icon"><i class="fa fa-calendar"></i>
                            </label>
    </label>
  </div>
</div>

关于Jquery查找下一个和上一个div行id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42336764/

相关文章:

javascript - Symfony:访问被拒绝时显示 jQuery 模式对话框

php - 使用 HTML 单选按钮和 javascript 进行评级

jQuery - 操作可排序列表中删除的元素

jquery DataTable动态添加行后将ID添加到TR元素

jQuery-AJAX |同一页面上有两个 ajax 帖子 | POST 后的问题

javascript - 在前面添加一个 div 并将其隐藏在该对象中

jquery - 如何在不使用项目选项的情况下向放大弹出窗口添加长描述?

javascript - jQuery 单击时,切换 next() 元素,但关闭所有同级元素

javascript - 阻止ajax请求,直到获得新 token

javascript - 仅选择与选择器匹配的父节点