我有一个下拉菜单,从具有foreach
循环的数组动态获取值。我知道javascript "getElementById"
需要一个唯一的键。问题是我的唯一密钥是"service_select"
和"$value2"
的组合。
我从下拉元素中得到的唯一的键是变量$value
。
<?php
echo'<select id="service_name" name="service_select">';
foreach($array_name_new as $key=>$value)
{
echo'<option value="'.$value.'">'.$value.'</option>';
}
echo'</select>';
echo'<p>Parameter: <input name="$value2" value="$value2'/></p>';
?>
对于每个选定的值在dropbox我想要一个"复选框"与dropbox的选择作为名称和值。我需要,虽然一个单独的文本字段与"$value2"
作为值。
我已经找到了这个线程(如何用javascript动态创建复选框列表),但我是javascript的新手,不完全理解代码。
- 函数
"function populate()"
中的if
子句是什么?是这是为了生成复选框吗? - 答案中的co分离在哪里被添加到原代码中?
根据提到的线程,我试图修改我的代码如下:
<?PHP
.
.
.
echo'<select id="service" name="service_select" onchange="add_service(this.id,$_POST['service_select'],$value2)">';
foreach($array_command_name_new as $key=>$value)
{
echo'<option value="'.$value.'">'.$value.'</option>';
}
echo'</select>';
$key2 = array_search($value,$command_in_hostfile[0]);
$value2 = $command_in_hostfile[1][$key2];
$id2 = compact("$_POST['service_select']", "value2");
<script type="text/javascript">
function add_service($id2, $_POST['service_select'], $value2)
{
foreach($array_command_name_new as $key=>$value)
{
var elementid = docuemnt.getElementById($id2);
var checkbox = document.createElement('id');
checkbox.type = "checked";
checkbox.name = "$_POST['service_select']";
checkbox.value = "$_POST['service_select']";
checkbox.id = "$id";
var label = document.createElement('$_POST['service_select']')
label.htmlFor = "id";
label.appendChild(document.createTextNode('$_POST['service_select']');
container.appendChild(checkbox);
container.appendChild(label);
}
echo'<p>Parameter: <input id="parameter" name="$value2" value="$value2' onclick="addService('value_parameter')" /> </p>';
var s1 = document.getElementById($id2);
}
</script>
.
.
.
?>
如果有人能帮助我,我将很高兴。
恐怕解决方案会更复杂一些…您需要为用户选择的每个值添加输入到表单中……
像这样:
[Option a] Parameter: [__________] [X]
[Option b] Parameter: [__________] [X]
[Please select... ][v]
每次用户选择一个新选项时,必须添加一个新行以允许输入参数。
你将需要在每行有一个[X]按钮,这样用户可以删除不需要的条目。
这是Javascript密集:)