我想要一些 jQuery 代码,当我单击文本框时,它可以让我找到控件的标签...所以在我的 HTML 中我有这个:
<label id="ctl00_WebFormBody_lblProductMarkup" for="ctl00_WebFormBody_txtPriceAdjustment">This Is My Label Value</label>
<input type="text" style="width:29px;" onclick="alert('label value here');" title="Here is a title" id="ctl00_WebFormBody_txtPriceAdjustment" maxlength="3" name="ctl00$WebFormBody$txtPriceAdjustment">
因此,当我单击文本框时,我想(例如)使用标签内的文本发出警报 - 因此在这种情况下,它会警报“这是我的标签值”
希望这是有道理的:)
使用属性选择器[]
like '[for="'+ this.id +'"]'
, where this.id
is the ID目前的focus
ed label
$("input").on("focus", function() {
const labelText = $('label[for="'+ this.id +'"]').text();
console.log( labelText );
});
<label for="inp">This Is My Label Value</label>
<input id="inp" type="text">
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)