将有多个部分使用.Ev2_PriceTypesRow,我想为每个部分运行一个脚本,但我遇到了选择器的问题。 我想要脚本说: 如果类" Ev2_PriceName的标签为BBFieldCaption"在班级" Ev2_PriceTypesRow"包含文本" Alumni"然后添加班级"校友"输入ID" P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Alumni</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Student</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Child</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Alumni Comp</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
&#13;
所以基本上我想在输入字段中添加一个类,如果标签文本包含&#34; Alumni&#34;则可以在代码片段中看到。 html是硬编码的,我不能直接编辑HTML b / c代码是由软件自动生成的,所以我需要使用JS。任何帮助将不胜感激。我已经尝试过使用.closest但是没有成功,因为我相信这会爬上DOM。
$('label:contains("Alumni")').closest('input').addClass('alumni');
答案 0 :(得分:1)
你想:
$('label:contains("Alumni")').parent().next().find('input').addClass('alumni');
$('label:contains("Alumni")').parent().next().find('input').addClass('alumni');
&#13;
.alumni {
color:red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Alumni</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Student</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Child</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
<div class="Ev2_PriceTypesRow">
<div class="Ev2_PriceTypesCell Ev2_PriceTypeNameColumn">
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblDesc_0" class="Ev2_PriceName BBFieldCaption">Alumni Comp</label>
<div>
<label for="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" class="Ev2_PriceCount">1 registrant</label>
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$ctl01" type="hidden" value="1" unit="10181" />
</div>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeQtyColumn">
<input name="P1212$ev2wiz$step1$rptevts$ctl00$ctlevt$rptPrice$ctl00$txtQty" type="text" id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_txtQty_0" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" />
</div>
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceItem_0" class="Ev2_PriceTypesCell Ev2_PriceTypeAmountColumn" style="display:none">
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_lblPrice_0">$0.00</span>
</div>
<div class="Ev2_PriceTypesCell Ev2_PriceTypeValidatorColumn">
<div id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_divPriceDescription_0" class="Ev2_PriceDescriptionCell"></div>
<span id="P1212_ev2wiz_step1_rptevts_ctlevt_0_rptPrice_0_ctl02_0" class="Ev2_Step1QtyValidation" style="visibility:hidden;">Please enter a whole number</span>
</div>
</div>
&#13;