最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Toggle between selected radio button using D3 - Stack Overflow

programmeradmin1浏览0评论

Consider I have 4 radio buttons as in 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?

Consider I have 4 radio buttons as in 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?

Share Improve this question edited Dec 10, 2016 at 12:01 Gayathri asked Dec 10, 2016 at 11:44 GayathriGayathri 1,9066 gold badges28 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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:

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

To assign it to a global (declaring without 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./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>

发布评论

评论列表(0)

  1. 暂无评论