2

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. The code that creates the html is working correctly 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:

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

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

Here's the problem, I can select more than one radio button at a time. 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. Any help is greatly appreciated!

4

3 回答 3

6

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

<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顺便说一句,属性以字母开头时会更好。

于 2013-06-14T21:21:01.747 回答
1

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

单选按钮分组由名称属性定义。您的按钮都有不同的名称,因此它们属于不同的组。

它应该是这样的:

<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>
于 2013-06-14T21:20:57.453 回答
1

尝试为单选按钮提供相同的名称。然后,您将只能选择一个单选按钮。

于 2013-06-14T21:22:16.047 回答