启用/禁用特定的动态添加的表单输入
我正在创建一个 HTML 表单,它从数据库 (php + mysql) 中获取一些选项。
在 php 中,我正在创建一个 checkbox
输入,旁边有一个选择框。
我将复选框命名为 houseAppsSelected[]
并选择 customCategories[]
,因此我将以数组形式获取值。
我将所有 HTML 附加到名为 $options
的 var 中,稍后再回显它。
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 动态构建它们,有没有办法让特定的复选框禁用/仅启用特定的选择框? (他们都有相同的名字)。
I'm creating an HTML form, which takes some of its options from a database (php + mysql)
In the php, I'm creating a checkbox
input, with a select box next to it.
I named the checkbox houseAppsSelected[]
and the select customCategories[]
, so I'll get the values as an array.
I append all the HTML into a var called $options
, and I echo it later on.
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 />";
}
I want to have the checkbox control whether the select box is enabled or disabled.
I found this article, which makes it look easy, but if all the select boxes have the same name, it will disable all of them.
Is there a way to have a specific checkbox disable/enable only a specific select box, if I build them dynamically with php? (they all have the same name).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
nextSibling
属性来查找选择。添加如下点击处理程序:
此处演示:http://jsfiddle.net/gilly3/vAK7N/
You can use the
nextSibling
property to find the select.Add the click handler like this:
Demo here: http://jsfiddle.net/gilly3/vAK7N/
您可以为每个标签赋予唯一的“id”值,该值独立于“名称”。然后您可以
通过该唯一值来访问它们。
您的 php 代码究竟如何构成唯一值取决于您的需要。它真的可以是任何东西。
You can give each tag a unique "id" value, which is independent of the "name". Then you can use
to access them by that unique value.
Exactly how your php code makes up the unique values sort-of depends on what you need, exactly. It can really be anything.