gpt4 book ai didi

javascript - 如何:If the label contains "Alumini" then add class to input

转载 作者:行者123 更新时间:2023-12-03 03:59:06 25 4
gpt4 key购买 nike

.Ev2_PriceTypesRow 将有多个部分,我想为每个部分运行一个脚本,但我在使用选择器时遇到了问题。我想让剧本说:如果类“Ev2_PriceTypesRow”中类“Ev2_PriceName BBFieldCaption”的标签包含文本“Alumni”,则将类“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 &amp;&amp; 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 &amp;&amp; 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 &amp;&amp; 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 &amp;&amp; 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>

所以基本上我想向输入字段添加一个类,如果标签文本包含“校友”,您可以在代码片段中看到该类。 html是硬编码的,我不能直接编辑HTML b/c代码是由软件自动生成的,所以我需要使用JS。任何帮助将不胜感激。我尝试过使用 .closest 但没有成功,因为我相信这会爬上 DOM。

$('label:contains("Alumni")').closest('input').addClass('alumni');

最佳答案

你想要:

$('label:contains("Alumni")').parent().next().find('input').addClass('alumni');

$('label:contains("Alumni")').parent().next().find('input').addClass('alumni');
.alumni {
color:red;
}
<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 &amp;&amp; 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 &amp;&amp; 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 &amp;&amp; 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 &amp;&amp; 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>

关于javascript - 如何:If the label contains "Alumini" then add class to input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44810349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com