【发布时间】:2015-01-14 13:29:31
【问题描述】:
我有这个代码:
<section class="col col-md-4">
<label class="checkbox">
<input type="checkbox" name="subscription" class="chk_Especialization" value="1">
</label>
</section>
<section class="col col-md-4">
<label class="input">
<input type="text" class="dateRangestart start dateRange" id="start-1" placeholder="dtStart">
</label>
</section>
<section class="col col-md-4">
<label class="input">
<input type="text" class="dateRangefinish finish dateRange" id="finish-1" placeholder="dtEnd">
</label>
</section>
<section class="col col-md-4">
<label class="checkbox">
<input type="checkbox" name="subscription" class="chk_Especialization" value="2">
</label>
</section>
<section class="col col-md-4">
<label class="input">
<input type="text" class="dateRangestart start dateRange" id="start-2" placeholder="dtStart">
</label>
</section>
<section class="col col-md-4">
<label class="input">
<input type="text" class="dateRangefinish finish dateRange" id="finish-2" placeholder="dtEnd">
</label>
</section>
<section class="col col-md-4">
<label class="checkbox">
<input type="checkbox" name="subscription" class="chk_Especialization" value="3">
</label>
</section>
<section class="col col-md-4">
<label class="input">
<input type="text" class="dateRangestart start dateRange" id="start-3" placeholder="dtStart">
</label>
</section>
<section class="col col-md-4">
<label class="input">
<input type="text" class="dateRangefinish finish dateRange" id="finish-3" placeholder="dtEnd">
</label>
</section>
如果我点击一个类开始输入元素,我想要下一个输入元素的警报 id,如果我点击一个完成类元素,我想要前一个输入元素的警报 id。
我尝试过使用这篇文章JQuery: Closest div that has an ID 和这篇文章jquery, find next element by class,但我认为我的代码中有一些错误。
这是javascript代码:
$(document).on("click", ".dateRange", function(){
if($(this).hasClass( "start" )){
//Select next input element with finish class
alert($(this).closest('input').next().attr("id"));
}
if($(this).hasClass( "finish" )){
//Select previous input element with start class
alert($(this).closest('input').prev().attr("id"));
}
});
我创建了一个 jsfiddle 到 :http://jsfiddle.net/towx8xro/1/
知道出了什么问题吗?
【问题讨论】:
-
为什么你的类中有斜线?
-
对不起,我是从我的 php 代码中复制过来的。我会解决这个问题的
-
可能也应该修复小提琴。
-
$(this).closest('input')???阅读文档:api.jquery.com/closest