【问题标题】:Insert radio with label using jQuery使用jQuery插入带有标签的收音机
【发布时间】:2016-10-18 07:45:39
【问题描述】:

我创建了一个 JS 测验,它将所有答案选项保存在数组中。然后将答案选项插入到创建的<input type="radio" . . . . /> 元素中。但是,选项不是在标签内,而是单独保存在输入元素内。这是为了进一步扩展我所说的内容:

我希望它是这样的

这是创建输入的代码:

  // Creates a list of the answer choices as radio inputs
  function createRadios(index) {
    var radioList = $('<ul>');
    var item;
    var input = '';
    for (var i = 0; i < questions[index].choices.length; i++) {
      item = $('<li>');
      input = '<input type="radio" name="answer" value=' + i + ' />';
      input += questions[index].choices[i];
      item.append(input);
    radioList.append(item);
    }
    return radioList;
  }

任何帮助将不胜感激。

【问题讨论】:

标签: javascript jquery html input radio


【解决方案1】:

标签应该真正包含最佳实践的单选按钮。试试这个:

// Creates a list of the answer choices as radio inputs
  function createRadios(index) {
    var radioList = $('<ul>');
    for (var i = 0; i < questions[index].choices.length; i++) {
      var item = $('<li>');
      var label = $('<label>');
      var input = $('<input>', {type: 'radio', name: 'answer', value: i});
      input.appendTo(label);
      label.append(questions[index].choices[i]);
      item.append(label);
      radioList.append(item);
    }
    return radioList;
  }

【讨论】:

  • 哇!非常感谢!!
猜你喜欢
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 2013-02-04
  • 2010-10-26
  • 2022-07-01
相关资源
最近更新 更多