在jQuery点击处理程序中获取选择器父ID
Get selector parent id in jQuery click handler
我想获取被点击的元素的父 id,所以在这种情况下要么#logo200_60_form
要么#logo400_120_form
。然后,我可以在其余代码中使用该 id。
我尝试了$(this).parent()
但这给了我直系父母,我需要上升几个级别。当 id 在单击处理程序有一种方法可以确定哪个父项导致了单击。
.JS:
$('#logo200_60_form a.fileinput-exists, #logo400_120_form a.fileinput- exists').on('click', function() {
$('form').get(0).reset();
$('#logo200_60').trigger('change');
$('#logo200_60_thumb').attr('src', 'http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image');
});
.HTML:
<form action="#" role="form" id="logo200_60_form">
<p>Please upload 200x60 png logo image</p>
<div class="form-group">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 60px;">
<img id="logo200_60_thumb" src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt="" />
</div>
<div id="logo200_60_preview" class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 60px;"></div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> Select image </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="..." id="logo200_60">
</span>
<a href="#" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>
</div>
</div>
</form>
<form action="#" role="form" id="logo400_120_form">
<p>Please upload 400x120 png logo image</p>
<div class="form-group">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 400px; height: 120px;">
<img id="logo400_120_thumb" src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt="" />
</div>
<div id="preview" class="fileinput-preview fileinput-exists thumbnail" style="max-width: 400px; max-height: 120px;"></div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> Select image </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="..." id="logo400_120">
</span>
<a href="#" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>
</div>
</div>
</form>
将closest()
与属性值选择器一起使用。
$(this) // The element that is clicked
.closest('form') // First <form> ancestor
.attr('id') // Get the value of `id` attribute
我尝试了$(this).parent(),但这给了我直接的父级,我需要上升几个级别。
你可以改用 .parents(),获取当前匹配元素集中每个元素的祖先,可以选择通过选择器过滤:
$(this).parents('form').attr('id');
希望这有帮助。
$('#logo200_60_form a.fileinput-exists, #logo400_120_form a.fileinput-exists').on('click', function() {
alert($(this).parents('form').attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#" role="form" id="logo200_60_form">
<p> Please upload 200x60 png logo image</p>
<div class="form-group">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 60px;">
<img id="logo200_60_thumb" src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt="" /> </div>
<div id="logo200_60_preview" class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 60px;"> </div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> Select image </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="..." id="logo200_60"
> </span>
<a href="#" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>
</div>
</div>
</form>
<form action="#" role="form" id="logo400_120_form" >
<p> Please upload 400x120 png logo image</p>
<div class="form-group">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 400px; height: 120px;">
<img id="logo400_120_thumb" src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt="" /> </div>
<div id="preview" class="fileinput-preview fileinput-exists thumbnail" style="max-width: 400px; max-height: 120px;"> </div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> Select image </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="..." id="logo400_120"
> </span>
<a href="#" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>
</div>
</div>
</form>
相关文章:
- 在动态创建的元素上获取对特定选择器的引用
- 使用javascript和css选择器获取value属性的内容
- 在jquery中获取累积选择器
- 如何从日期范围选择器中获取两个日期
- 获取PHP代码中日期选择器的值
- Jquery-如何获取插件调用的选择器
- 引导时间选择器在增加或减少小时和分钟的同时获取 NaN
- 使用本机 Javascript 从 DOM 元素获取选择器字符串
- 在jQuery点击处理程序中获取选择器父ID
- 如何使用javascript或jquery在switch语句中获取选择器类型
- 使用 jQuery 获取选择器
- 仅获取输入类型文本,而不获取选择器字段
- 如何使用钛合金在控制器中获取选择器值
- JQuery如何使用x2.next()函数获取选择器
- 如何获取选择器使用find()函数获得的无线电输入值
- 是否可以使用indexOf获取选择器
- 如何获取选择器中的所有对象
- 在cq5中获取选择器
- js,如何从元素获取选择器
- 获取< >选择器使用jquery