javascript - 选择其他按钮时,JQuery Mobile 单选按钮保持选中状态。使用 .html 函数

标签 javascript html jquery jquery-mobile radio-button

我似乎无法解决这个问题,也找不到任何相关信息。我正在使用 .html() 函数生成动态 HTML,以单选按钮格式列出相机选择。 创建 html 的代码工作正常 AFAIK:

var showcameras = function()
{
    var html = "<fieldset data-role='controlgroup' data-type='horizontal'>";
    for (var i = 0; i < sessiondetails["Camera-Count"]; i++)
    {
        var name;
        var namelabel = "Camera-Names_" + i;
        var idlabel = "Camera-ID_" + i;
        if (sessiondetails[namelabel]==="")
            name = "Camera " + (i+1);
        else
            name = sessiondetails[namelabel];
        html += "<input type='radio' name='"+name+"' id='"+sessiondetails[idlabel]+"' value='"+sessiondetails[idlabel]+"'>";
        html += "<label for='"+sessiondetails[idlabel]+"'>"+name+"</label>";
    }
    html += "</fieldset>";
    window.$("#camselection").html(html).trigger("create");
};

这是它创建的 HTML:

    <fieldset data-role='controlgroup' data-type='horizontal'>
    <input type='radio' name='test1' id='10000' value='10000'>
    <label for='10000'>test1</label>
    <input type='radio' name='Camera 2' id='10001' value='10001'>
    <label for='10001'>Camera 2</label>
    <input type='radio' name='Camera 3' id='10002' value='10002'>
    <label for='10002'>Camera 3</label>
    <input type='radio' name='Camera 4' id='10003' value='10003'>
    <label for='10003'>Camera 4</label>
    </fieldset>

这是“camselection”div 标签的 HTML:

    <div id="camselection" data-role="fieldcontain"></div>

问题来了,我一次可以选择多个单选按钮。对我来说没有意义,因为它们在同一个字段集中。我什至尝试将 onClick('refresh') 放在每个按钮上,但它不起作用。非常感谢任何帮助!

最佳答案

为了只选择一个单选按钮,它们必须具有相同的名称。

<fieldset data-role='controlgroup' data-type='horizontal'>
  <input type='radio' name='camera' id='c10000' value='10000'>
  <label for='c10000'>test1</label>
  <input type='radio' name='camera' id='c10001' value='10001'>
  <label for='c10001'>Camera 2</label>
  <input type='radio' name='camera' id='c10002' value='10002'>
  <label for='c10002'>Camera 3</label>
  <input type='radio' name='camera' id='c10003' value='10003'>
  <label for='c10003'>Camera 4</label>
</fieldset>

顺便说一句,id 属性以字母开头会更好。

关于javascript - 选择其他按钮时,JQuery Mobile 单选按钮保持选中状态。使用 .html 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17117384/

相关文章:

javascript - 计算页面加载的所有资源

javascript - Google Optimize 中的 Javascript 变量定位中的全局变量名称是什么?

html - 在每个 "!"字符后插入换行符

JavaScript/jQuery JSON 数组问题 - 值未定义

jquery - 在 jQuery 中,如何指定仅在特定区域执行某个功能?

JavaScript/jQuery 类修改

javascript - 隔离范围指令导致 karma 错误

javascript - 如何通过javascript设置图像尺寸? (通过 JS 提供的 SSL Logo ,无尺寸)

html - ioslides 中 Shiny 的应用程序大小

javascript - 在 audio.js 中禁用快进音频