0

我有下表是为 MySQL 表中的每个来宾条目构建的。

    echo '<tr>
            <td>'.$gastId[$i]." ".$voornaam[$i].'</td>
            <td><input type="radio" name=present'.$gastId[$i].'[] value=1 onclick="setReadOnly(this)" checked></td>
            <td><input type="radio" name=present'.$gastId[$i].'[] value=2 onclick="setReadOnly(this)"></td>
            <td><input type="text"  name="artiest[]" value="'.$artiest[$i].'"></td>
            <td><input type="text"  name="titel[]" value="'.$titel[$i].'"></td>
        </tr>';

我想根据存在的单选按钮'.$gastId[$i]禁用两个字段artiest(艺术家)和(标题)。titel

我试图用 javascript 解决它,但我对 javascript 的经验很少。有没有办法通过 ID 或其他方式链接每行的单选按钮和文本字段?

<script language='javascript'>
<!-- //
function setReadOnly(obj)
{
    if(obj.value == 1)
    {
        document.forms[0].artiest.readOnly = 0;
    } else {
        document.forms[0].artiest.readOnly = 1;
    }
}
// -->
</script>

非常感谢您的帮助!

编辑 21-10-2012

对不起,我应该更清楚。

如果表格如下所示:

------------------------------------------------
|  P  |  NP  | Artist        | Title           |
------------------------------------------------
|  X  |      | Enabled       | Enabled         |
------------------------------------------------
|     |  X   | Disabled      | Disabled        |
------------------------------------------------
etc

因此,该行的单选按钮控制是否启用或禁用同一行中的字段。

4

2 回答 2

1

只需将 ID 添加到:

<td><input type="text"  id="artiest" name="artiest[]" value="'.$artiest[$i].'"></td>
                        ^^^^^^^^^^^
于 2012-10-20T20:55:45.953 回答
0

我已经开始工作了:

我已将 ID 字段添加到元素中。

    echo '<tr>
            <td>'.$gastId[$i]." ".$voornaam[$i].'</td>
            <td><input type="radio" id='.$gastId[$i].' name=present'.$gastId[$i].'[] value=1 onclick="setReadOnly(this)" checked></td>
            <td><input type="radio" id='.$gastId[$i].' name=present'.$gastId[$i].'[] value=2 onclick="setReadOnly(this)"></td>
            <td><input type="text"  id=artiest'.$gastId[$i].' name="artiest[]" value="'.$artiest[$i].'"></td>
            <td><input type="text"  id=titel'.$gastId[$i].'   name="titel[]" value="'.$titel[$i].'"></td>
        </tr>';

并更改了javascript:

function setReadOnly(obj)
{
    if(obj.value == 1)
    {
        document.getElementById("artiest"+obj.id).readOnly = 0;
        document.getElementById("titel"+obj.id).readOnly = 0;
    } else {
        document.getElementById("artiest"+obj.id).readOnly = 1;
        document.getElementById("titel"+obj.id).readOnly = 1;
    }
}
// -->
</script>

谢谢大家的帮助!

于 2012-10-21T09:52:13.097 回答