[英]jQuery to find parent label
I have this HTML fragment: 我有这个HTML片段:
<div class="form-group">
<div class="col-sm-1"></div>
<label for="allquestion4" class="col-sm-6 control-label label-red">Question</label>
<div class="col-sm-1"></div>
<div class="col-sm-3">
<select size="2" class="selectpicker" id="allquestion4" title="Choose">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</div>
<div class="col-sm-1"></div>
</div>
Why is the label not being found by the following jQuery: 为什么以下jQuery无法找到标签:
$(this).closest('label').hasClass('.label-red');
Where $(this) is the select picker. 其中$(this)是选择器。
UPDATE 更新
Remove the .
删除.
from hasClass('.label-red')
. 来自hasClass('.label-red')
。 It wants the name of the class not a CSS selector. 它想要类的名称而不是CSS选择器。
There is an error in the original JQuery selector as pointed out by @Mark remove the .
@Mark指出,原始JQuery选择器中存在错误,请删除.
from the hasClass('.label-red')
secondly the selector needs a parent so the html needs to be modified so that the <label>
tags wrap the <select>
thus: 其次,从hasClass('.label-red')
选择器需要一个父类,因此需要修改html,以便<label>
标签将<select>
包装起来,从而:
<label class="col-sm-6 control-label label-red">Question
<select size="2" class="selectpicker" id="allquestion4" title="Choose">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</label>
Or if the html is kept the same navigate to the parent form-group
and then find like this: 或者,如果html保持不变,请导航到父form-group
,然后找到以下内容:
$(this).closest('.form-group').find('select').hasClass('label-red')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.