简体   繁体   English

如何获取所选单选按钮的值并将其传递给另一个变量?

[英]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.

相关问题 如何使用 Javascript 和 Z3EB7106C3477A60E6BBF5DBFDE1DA 将 HTML 单选按钮值传递给 PHP 变量? - How do I pass HTML radio button value to a PHP variable using Javascript and Ajax? 如何在angularjs中获取所选单选按钮的值? - How do i Get value of selected radio button in angularjs? 如果我单击选择按钮并在文本框中传递其值,如何获得所选单选按钮的值? - how can i get the value of selected radio button if i click the select button and pass its value in a textbox? 选择单选按钮后,如何使用其值动态更新另一个 PHP 变量 - Upon selecting a radio button, how do I use its value to dynamically update another PHP variable 如何通过单选按钮将所选值传递给javascript函数 - How to pass selected value to javascript function from radio button 如何根据选定的单选按钮值传递单选按钮值? - How can i pass radio button values based on the selected one? 单击按钮后,我可以在脚本中获取所选单选按钮的值吗? - can i take value of selected radio button in scriplet after clicking a button 如何获得控制器中用户选择的单选按钮的值? - How do I get the value of a user-selected radio button in the controller? 当存储的值=“ on”时,如何在启动时将html单选按钮显示为选中状态? - How do I make an html radio button display as selected at launch time when the stored value=“on”? 如何在 Ajax 中将按钮值作为变量传递? - How do I pass Button Value as a variable in Ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM