简体   繁体   中英

jQuery - Get value of radio button text

I'm a beginner to programming. I'm building a quiz app with HTML, CSS, jQuery and Javascript. I'm trying to obtain the value of a radio button text with the following jQuery code:

jQuery:

$('input[name="radios"]:checked').val()

HTML:

<input type="radio" id="A" name="radios" value="France"><label for="r1">France</label>

I've also changed the value of the radio button once the user clicks submit:

jQuery

$("label[for=r1]").html("Germany")

However, the next time I run the same jQuery command to get the value of the radio button text, it returns to me the earlier value and not the new value. Is there something I am missing in the code?

you get and set an input's value with the same function.

//Gets a value
$('input[name="radios"]:checked').val()
//Sets a value
$('input[name="radios"]:checked').val("some new value")

使用.text()函数更改标签的值。

$("label[for=r1]").text("Germany")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM