如何使用 jquery 中的父函数获取 class 名称
How to get the class name with parent function in jquery
如何在单击函数 addIncludeTextBox
时获得 'field_wrapper_1'
class 名称?
<div class="field_wrapper_1">
<span class="error php-error" id="php-error_includes" style="display: none"></span>
<div class="row">
<div class="col-md-11">
<div class="form_group">
<label class="form_label">Includes <span>*</span></label>
<input class="form_input" name="includes[][]" id="includes" placeholder="Enter the details of fees includes" type="text">
</div>
</div>
<div style="padding-top: 3.6%;" class="col-md-1">
<a href="javascript:void(0);" class="add_button" onclick="addIncludeTextBox(this)" title="Add field"><i class="fa fa-plus" aria-hidden="true"></i></a>
</div>
</div>
</div>
你可以这样做:
$(obj).closest('[class^=field_wrapper]').attr("class")
合并 .closest
和 .attr
得到 class。
可选你也可以只使用.closest('.field_wrapper_1')
工作演示
function addIncludeTextBox(obj){
console.log($(obj).closest('[class^=field_wrapper]').attr("class"))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field_wrapper_1">
<span class="error php-error" id="php-error_includes" style="display: none"></span>
<div class="row">
<div class="col-md-11">
<div class="form_group">
<label class="form_label">Includes <span>*</span></label>
<input class="form_input" name="includes[][]" id="includes" placeholder="Enter the details of fees includes" type="text">
</div>
</div>
<div style="padding-top: 3.6%;" class="col-md-1">
<a href="javascript:void(0);" class="add_button" onclick="addIncludeTextBox(this)" title="Add field"><i class="fa fa-plus" aria-hidden="true">+</i></a>
</div>
</div>
</div>
如何在单击函数 addIncludeTextBox
时获得 'field_wrapper_1'
class 名称?
<div class="field_wrapper_1">
<span class="error php-error" id="php-error_includes" style="display: none"></span>
<div class="row">
<div class="col-md-11">
<div class="form_group">
<label class="form_label">Includes <span>*</span></label>
<input class="form_input" name="includes[][]" id="includes" placeholder="Enter the details of fees includes" type="text">
</div>
</div>
<div style="padding-top: 3.6%;" class="col-md-1">
<a href="javascript:void(0);" class="add_button" onclick="addIncludeTextBox(this)" title="Add field"><i class="fa fa-plus" aria-hidden="true"></i></a>
</div>
</div>
</div>
你可以这样做:
$(obj).closest('[class^=field_wrapper]').attr("class")
合并 .closest
和 .attr
得到 class。
可选你也可以只使用.closest('.field_wrapper_1')
工作演示
function addIncludeTextBox(obj){
console.log($(obj).closest('[class^=field_wrapper]').attr("class"))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field_wrapper_1">
<span class="error php-error" id="php-error_includes" style="display: none"></span>
<div class="row">
<div class="col-md-11">
<div class="form_group">
<label class="form_label">Includes <span>*</span></label>
<input class="form_input" name="includes[][]" id="includes" placeholder="Enter the details of fees includes" type="text">
</div>
</div>
<div style="padding-top: 3.6%;" class="col-md-1">
<a href="javascript:void(0);" class="add_button" onclick="addIncludeTextBox(this)" title="Add field"><i class="fa fa-plus" aria-hidden="true">+</i></a>
</div>
</div>
</div>