[英]How do I take the value of a selected radio button and pass it to another variable?
I'm learning JS/HTML/CSS and need some help with a project. 我正在学习JS / HTML / CSS,并且需要一些项目帮助。 I'm trying to create 4 buttons, and when one is selected, I want its text value to be saved to a variable that I can then use later on down the page.
我正在尝试创建4个按钮,当选择一个按钮时,我希望将其文本值保存到一个变量中,以便稍后在页面下方使用。 I'm mostly not sure how to write a function that determines which button was collected and then saves the value to a variable (ie 1 room, 2 rooms)
我大多不确定如何编写一个函数来确定收集了哪个按钮,然后将值保存到变量(即1个房间,2个房间)
<button id = "numberrooms1" onclick="myFunction()">1 room</button>
<button id = "numberrooms2" onclick="myFunction()">2 rooms</button>
<button id = "numberrooms3" onclick="myFunction()">3 rooms</button>
<button id = "numberrooms4" onclick="myFunction()">4 rooms</button>
<script type="text/javascript">
var x = document.getElementById("numberrooms1").textContent;
</script>
Later on I want to reference the variable here... 稍后我想在这里引用变量...
<script type="text/javascript">
var myFunction8 = function(){
document.getElementById('response').innerHTML = ("My name is " + yourName + " and I'm subletting my " + x + " bedroom apartment.")
};
</script>
Add a class to the buttons and you can set all the handlers in one place: 在按钮上添加一个类,您可以将所有处理程序放在一个位置:
<button class="numberrooms" id="numberrooms1">1 room</button>
<button class="numberrooms" id="numberrooms2">2 rooms</button>
<button class="numberrooms" id="numberrooms3">3 rooms</button>
<button class="numberrooms" id="numberrooms4">4 rooms</button>
var buttons = document.getElementsByClassName('numberrooms');
for ( var i = 0; i < buttons.length; ++i )
buttons[i].addEventListener('click', myFunction);
Then, in myFunction()
, use this
to get the button that was clicked, and get the innerHTML
from that: 然后,在
myFunction()
,使用this
来获取被单击的按钮,并从中获取innerHTML
:
var x;
function myFunction(){
x = this.innerHTML;
}
var buttons = document.getElementsByClassName('numberrooms'); for (var i = 0; i < buttons.length; ++i) buttons[i].addEventListener('click', myFunction); var x; function myFunction() { x = this.innerHTML; document.getElementById('response').innerHTML = x; }
<button class="numberrooms" id="numberrooms1">1 room</button> <button class="numberrooms" id="numberrooms2">2 rooms</button> <button class="numberrooms" id="numberrooms3">3 rooms</button> <button class="numberrooms" id="numberrooms4">4 rooms</button> <p id="response"></p>
So the first thing you will want to do is add a value tag to your buttons. 因此,您要做的第一件事就是在按钮上添加一个value标签。
<button id = "numberrooms1" onclick="myFunction()" value="1">1 room</button>
<button id = "numberrooms2" onclick="myFunction()" value="2">2 rooms</button>
The Second thing you will want to do is declare a variable within your script tag. 您要做的第二件事是在script标记中声明一个变量。
<script type="text/javascript">
var rooms;
var myFunction8 = function(){
document.getElementById('response').innerHTML = ("My name is " + yourName + " and I'm subletting my " + x + " bedroom apartment.")
};
</script>
Then you will create your onclick function which will set the variable to your desired value. 然后,您将创建onclick函数,该函数会将变量设置为所需的值。
var myOnclickFunction = function(button){
rooms = button.value;
};
You will also want to call your onclick function using this. 您还需要使用此函数来调用onclick函数。
<button id = "numberrooms1" onclick="myOnclickFunction(this)" value="1">1 room</button>
<button id = "numberrooms2" onclick="myOnclickFunction(this)" value="2">2 rooms</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.