简体   繁体   English

如何使用javascript在同一行中制作单选按钮和标签

[英]How to make radio button and label in the same line using javascript

I am trying to add radio buttons through javascript dynamically as the number of radio buttons is determined run time.我正在尝试通过 javascript 动态添加单选按钮,因为单选按钮的数量是在运行时确定的。 When I create radio buttons like this, radio button is getting displayed on top of label.当我创建这样的单选按钮时,单选按钮显示在标签顶部。 But I want label to be displayed next to the radio button.但我希望标签显示在单选按钮旁边。 Below is the javascript code.下面是javascript代码。

  x = matchInfoArray[0];
  var values = [x.team1, x.team2];

  var selecttag1=document.createElement("input");
  selecttag1.setAttribute("type", "radio");
  selecttag1.setAttribute("name", "match1");
  selecttag1.setAttribute("value", values[0]);
  selecttag1.setAttribute("id","match1");
  var lbl1 = document.createElement("label");
  lbl1.innerHTML = values[0];
  div = document.getElementById("poll-content");
  div.appendChild(selecttag1);
  div.appendChild(lbl1);
  
  selecttag1=document.createElement("input");
  selecttag1.setAttribute("type", "radio");
  selecttag1.setAttribute("name", "match1");
  selecttag1.setAttribute("value", values[1]);
  selecttag1.setAttribute("id","match1");
  lbl1 = document.createElement("label");
  lbl1.innerHTML = values[1];
  div = document.getElementById("poll-content");
  div.appendChild(selecttag1);
  div.appendChild(lbl1);

'poll-content' is a div and css is as below. 'poll-content' 是一个 div,css 如下。

 .poll-content input[type="button"]
{
    border: none;
    margin-top: 100px;
    outline: none;
    height: 30px;
    width: 250px;
    background: #fb2525;
    color: #fff;
    font-size: 18px;
    border-radius: 20px;
}

Can any one please help me how to fix this issue through javascript.任何人都可以帮助我如何通过javascript解决这个问题。

for this kind of code, I prefer to use the DOMParser, it allows me to be sure of the result, and the code is cleaner, easier to correct or maintain.对于这种代码,我更喜欢使用DOMParser,它可以让我确定结果,并且代码更清晰,更容易更正或维护。

 const DomParser = new DOMParser() , pollContent = document.getElementById("poll-content") ; function makeLabelInput( val, no) { let Elms = ` <label> ${val} <input type="radio" name="match${no}" value="${val}"> </label> ` return (DomParser.parseFromString( Elms, 'text/html')).body.firstChild } const matchInfoArray = [ { team1: 'team1', team2:'team2' }] let indx = 0 pollContent.appendChild( makeLabelInput( matchInfoArray[indx].team1, indx) ) pollContent.appendChild( makeLabelInput( matchInfoArray[indx].team2, indx) )
 #poll-content label { margin-right: .5em; outline: none; padding: .2em .3em .2em .8em; background: #fb2525; color: #fff; border-radius: 1em; } #poll-content input[type=radio] { transform: translateY(.2em); }
 <div id="poll-content"></div>

No doubt, Mr Jojo's answer works and is probably the right answer here.毫无疑问,乔乔先生的回答有效,可能是这里的正确答案。 However, I would like to point out that you can basically achieve a similar result with much less effort:但是,我想指出的是,您基本上可以以更少的努力获得类似的结果:

 const matchInfoArray = [ { team1: 'team1', team2:'team2' }]; document.getElementById("poll-content").innerHTML+= Object.values(matchInfoArray[0]).map(val=> `<label> ${val} <input type="radio" name="match0" value="${val}"> </label>`).join("");
 #poll-content label { margin-right: .5em; outline: none; padding: .2em .3em .2em .8em; background: #fb2525; color: #fff; border-radius: 1em; } #poll-content input[type=radio] { transform: translateY(.2em); }
 <div id="poll-content"></div>

In my version I directly append the HTML to the already existing HTML of the target element.在我的版本中,我直接将 HTML 附加到目标元素的现有 HTML。 This can have the undesired effect of unbinding events from already existing children in the target.可能会产生从目标中已经存在的子项解除绑定事件的不良影响。 But, in case you have not used any direct attachment bindings on them then this method is a fast and easy way of creating the elements in question.但是,如果您没有对它们使用任何直接附件绑定,那么此方法是创建相关元素的一种快速简便的方法。 In the above snippet I skipped the parametrisation of indx and hard coded it as 0 , as only this index seems to exist, but you can of course just as well wrap the expression with a .map() over all indx of the matchInfoArray elements.在上面的代码中我忽略的参数化indx和硬编码它作为0 ,因为只有这个指标似乎存在,但你当然可以一样好了包裹式.map()在所有indx的的matchInfoArray元素。

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

相关问题 如何使用jQuery或javascript使选定的单选按钮标签的文本加粗? - How to make the text of selected radio button label bold using jQuery or javascript? 如何使用 javascript 获取所选单选按钮的 label - How do I get the label of the selected radio button using javascript JavaScript如何更改单选按钮标签文本? - Javascript how to change radio button label text? 一旦选中了另一个单选按钮,如何使单选按钮和相关的 label 消失? (Javascript) - How do you make a radio button and associated label disappear once another radio button has been checked? (Javascript) 如何使用标签取消选中jQuery中的单选按钮 - How to uncheck radio button in jquery using label 在JavaScript中,如何使它悬停在标签上并在ap标签中输出单选按钮的值? - In JavaScript, how do I make it so hovering over the label will output in a p tag the value of the radio button? JavaScript 创建一个单选按钮和一个 label - JavaScript creating a radio button and a label 如何使用javascript使按钮标签动态化? - How can I make button label dynamic using javascript? 如何使用 javascript 获取多个单选按钮具有相同名称的选定单选按钮的值 - How to get the value of selected radio button where multiple radio buttons have same name using javascript 使用javascript / jQuery返回单选按钮的标签值 - Returning the label value of a radio button using javascript/jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM