简体   繁体   English

JQuery 移动单选按钮在选择其他按钮时保持选中状态。 使用.html function

[英]JQuery Mobile Radio Buttons stay selected when selecting other buttons. Using .html function

I can't seem to figure this out, or find any information on this.我似乎无法弄清楚这一点,或找到有关此的任何信息。 I'm using the.html() function to generate dynamic HTML to list Camera selection in a radio button format.我正在使用 .html() function 生成动态 HTML 以单选按钮格式列出相机选择。 The code that creates the html is working correctly AFAIK:创建 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");
};

This is the HTML it creates:这是它创建的 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>

This is the HTML for the "camselection" div tag:这是“camselection”div 标签的 HTML:

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

Here's the problem, I can select more than one radio button at a time.这是问题所在,我一次可以 select 多个单选按钮。 Doesn't make sense to me since they are in the same fieldset.对我来说没有意义,因为它们在同一个字段集中。 I've even tried putting onClick('refresh') on each button, but it doesn't work.我什至尝试将 onClick('refresh') 放在每个按钮上,但它不起作用。 Any help is greatly appreciated任何帮助是极大的赞赏

In order to select only one radio button they must have the same name.为了 select 只有一个单选按钮,它们必须具有相同的名称。

<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>

BTW it is better when id attribute is starting with a letter.顺便说一句,当id属性以字母开头时会更好。

http://www.w3schools.com/html/html_forms.asp http://www.w3schools.com/html/html_forms.asp

The radio button grouping is defined by the name attribute.单选按钮分组由名称属性定义。 Your buttons all have different names, so they are part of different groups.您的按钮都有不同的名称,因此它们属于不同的组。

It should be something like this:它应该是这样的:

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

Try giving the radio buttons the same name.尝试为单选按钮提供相同的名称。 Then you'll be able to select only one radio button.然后你就可以 select 只有一个单选按钮。

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

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