Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

html - Get Text of checkbox with Jquery

<input type="checkbox" name="PrePayment">Pre-Payment<br />

How would I get the text "Pre-Payment" from this using Jquery?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'd recommend putting the text inside a <label> tag so that you could click on it (and so that screen readers and such could make sense of your form):

<input type="checkbox" name="PrePayment" id="pre-payment">
<label for="pre-payment">Pre-Payment</label>
<br />

Then, the whole thing becomes easy:

var text    = $('label[for=pre-payment]').text();
var or_this = $('#pre-payment').next('label').text();

I'd prefer the first option, label[for=...], as it is less fragile than the second


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...