简体   繁体   English

使用D3在选定的单选按钮之间切换

[英]Toggle between selected radio button using D3

Consider I have 4 radio buttons as in html 考虑我有4个单选按钮,如html

<form id="form">
    <input type="radio" name="stack" value="north">north<br>
    <input type="radio" name="stack" value="east" >east<br>
    <input type="radio" name="stack" value="west">west<br>
    <input type="radio" name="stack" value="south">south
</form>

How do I get the value of selected radio button and assign onto a global variable, on selection , using D3.js ,such a way I can toggle between selections? 如何在选择时使用D3.js获取所选单选按钮的值并分配给全局变量,这样我就可以在选择之间进行切换?

If those are the only inputs you have, you can do: 如果只有这些输入,则可以执行以下操作:

d3.selectAll("input").on("change", function(){
    console.log(this.value)
});

To select only that specific group of radio buttons, use their name : 要仅选择特定的单选按钮组,请使用其name

d3.selectAll("input[name='stack']").on("change", function(){
    console.log(this.value)
});

To assign it to a global (declaring without var ): 要将其分配给全局(不使用var声明):

d3.selectAll("input[name='stack']").on("change", function(){
    globalVariable = this.value;
});

Here is the demo: 这是演示:

 d3.selectAll(("input[name='stack']")).on("change", function(){ console.log(this.value) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <form id="form"> <input type="radio" name="stack" value="north">north<br> <input type="radio" name="stack" value="east" >east<br> <input type="radio" name="stack" value="west">west<br> <input type="radio" name="stack" value="south">south </form> 

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

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