简体   繁体   English

jQuery-获取单选按钮文本的值

[英]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. 我正在使用HTML,CSS,jQuery和Javascript构建测验应用程序。 I'm trying to obtain the value of a radio button text with the following jQuery code: 我正在尝试使用以下jQuery代码获取单选按钮文本的值:

jQuery: jQuery的:

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

HTML: 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 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. 但是,下次我运行相同的jQuery命令以获取单选按钮文本的值时,它将返回给我以前的值,而不是新的值。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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