简体   繁体   中英

d3js: Creating multiple radio buttons

I'm trying to create two radio buttons using d3, however I seem to be having some issues. I can create and label both, however they don't recognize that the other one has been created. For example, if the OR button is selected and I select the AND button, the OR button won't unselect.

I was able to recreate my problem in a fiddle and the code is below:

var body = d3.select("body")

var form = body.append('form');

form.append('input')
    .attr('type', 'radio')
    .attr('value', 'Or')
    .on('click', function () {
        //Do something
    });

form.append('label')
    .html('OR');

form.append('input')
    .attr('type', 'radio')
    .attr('value', 'And')
    .on('click', function () {
        //Do something
    });

form.append('label')
    .html('AND');

The problem was fixed by assigning both radio buttons a name.

See the updated fiddle here

var body = d3.select("body")

var form = body.append('form');

form.append('input')
    .attr('type', 'radio')
    .attr('value', 'Or')
    .attr('name', 'toggle')
    .on('click', function () {
        //Do something
    });

form.append('label')
    .html('OR');

form.append('input')
    .attr('type', 'radio')
    .attr('value', 'And')
    .attr('name', 'toggle')
    .on('click', function () {
        //Do something
    });

form.append('label')
    .html('AND');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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