简体   繁体   中英

Radio button inside div

I have a form html which can add the form dynamically. I have problem with radio button, it can only select one option (even when the radio button is in different tag). I search the solution, one solution is to change the name of the radio button. Is there any other way to do it? Because my other input all use same name and when submit the form all input can be collected in an array.

If there's no other way, how can I change the name of the radio button for the cloned element?

Here's my code:

app.get('/form', function (req, res) {
var html='';
html += `<head>
<style>
  .formset {
  border-style: solid;
  border-width: 1px;
  }
</style>
<script>
var id=1
function myFunction() {
  id++
  var elmnt = document.getElementById("div1")
  var cln = elmnt.cloneNode(true)
  cln.id = "div"+id
  document.getElementById("divParent").appendChild(cln);
}
</script>
</head>`;
html +="<body>";
html += "<p>Click the button to make a BUTTON element with text.</p>";
html += '<button onclick="myFunction()">Add form</button>';
html += "<form action='/thank'  method='post' name='form1'>";
html += "<div id='divParent'>";
html += "<div id='div1' name='formset' class='formset'>";
html += "<p>Name: <input type= 'text' name='name'></p>";
html += "<p>Sex: Male <input type='radio' name='gender' value='male'>"
html += " Female <input type='radio' name='gender' value='female'></p>"
html += "<p>Email: <input type='text' name='email'></p>";
html += "<p>Address:<input type='text' name='address'></p>";
html += "<p>Mobile number:<input type='text' name='mobilno'></p>";
html += "</div>";
html += "</div>";
html += "<input id='input1' type='submit' value='submit'>";
html += "<INPUT type='reset'  value='reset'>";
html += "</form>";

html += "</body>";
res.send(html);
});

And the code to handle the form:

app.post('/thank', urlencodedParser, function (req, res){
var reply='';
reply += "<br>Array length is : " + req.body.name.length;
reply += "<br>Your name is : " + req.body.name;
reply += "<br>Sex is : " + req.body.gender;
reply += "<br>Your E-mail id is : " + req.body.email; 
reply += "<br>Your address is : " + req.body.address;
reply += "<br>Your mobile number is : " + req.body.mobilno;
res.send(reply);
});

I think you need change name of the radio input, add class to radio to make it can be found by getElementsByClassName.

html += "<p>Sex: Male <input type='radio' class='gender' name='gender' value='male'>"
html += " Female <input type='radio' class='gender' name='gender' value='female'></p>"

//change the name, add id to the end
function changeName(element, id) {
    var x = element.getElementsByClassName("gender");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].name += id;
    }
}

function myFunction() {
    id++
    var elmnt = document.getElementById("div1")
    var cln = elmnt.cloneNode(true)
    cln.id = "div"+id

    //change radio id
    changeName(cln, id);

    document.getElementById("divParent").appendChild(cln);
}

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