启用/禁用特定的动态添加的表单输入


Enable/Disable a specific dynamically-added form-input

我正在创建一个HTML表单,它从数据库(php + mysql)中获取一些选项

在php中,我创建了一个checkbox输入,旁边有一个选择框。

我将复选框命名为houseAppsSelected[],并选择customCategories[],因此我将以数组的形式获得值。

我将所有HTML附加到一个名为$options的变量中,并在稍后返回它。

while ($row=mysql_fetch_array($result)) { 
    $cat_id=$row["category_id"]; 
    $cat_name=$row["category_name"]; 
    $options.="<INPUT type='"checkbox'" name='"houseAppsSelected[]'" VALUE='"$cat_id'">".$cat_name." ---> ";

    $custom_sql="SELECT custom_cat_id, cat_name FROM custom_categories WHERE house_app='$cat_id'"; 
    $custom_result=mysql_query($custom_sql);
    $options.="<SELECT name='"customCategories[]'">";
    $options.="<OPTION value='"0'"> Choose Category </option>";
    while ($custom_row=mysql_fetch_array($custom_result)) {
        $custom_id = $custom_row['custom_cat_id'];
        $custom_name = $custom_row['cat_name'];
        $options.="<OPTION value='"$custom_id'">".$custom_name."</option>";
    }
    $options.="</SELECT> <br /> <br />";
}

我想让复选框控制选择框是启用还是禁用

我找到了这篇文章,它看起来很简单,但如果所有的选择框都有相同的名称,它将禁用所有的。

是否有一种方法有一个特定的复选框禁用/启用只有一个特定的选择框,如果我用php动态构建它们?(他们都有相同的名字)

您可以使用nextSibling属性来查找选择

function chkboxClick(chkbox) {
    chkbox.nextSibling.nextSibling.disabled = !chkbox.checked;
}

添加点击处理程序,如下所示:

<INPUT type="checkbox" onclick="chkboxClick(this)" ... /> 

此处演示:http://jsfiddle.net/gilly3/vAK7N/

你可以给每个标签一个唯一的"id"值,它独立于"name"。然后可以使用

var elem = document.getElementById(something);

来访问它们。

确切地说,你的php代码是如何组成唯一值的,这取决于你需要什么。它可以是任何东西