最近我已经问了一个similar question,但现在我的表单是动态的,所以代码与前一个问题的代码不一样,我在解决问题时遇到了一些困难。
我有一个代码,可以为表单添加一些选项,其中包含下拉列表“类别”字段,其中动态收集类别。我想要的是以PHP特定的方式添加<option selected disabled>Select a category ...</option>
作为该字段的第一个选项。
我的实际代码:
for( $i = 0; $i < $count; $i++ ) {
if($form["field"][$i]["name"] == "advert_category") {
$form["field"][$i]["max_choices"] = 1;
// This is what I tried
//$form["field"][$i]["options"] = array(array("value" => " ", "text" => "Select a category ...", "depth" => 0, "selected" => "selected", "disabled" => "disabled"));
}
}
答案 0 :(得分:1)
// append
$form["field"][$i]["options"][] = array("value" => " ", "text" => "Select a category ...", "depth" => 0, "selected" => "selected", "disabled" => "disabled");
// or prepend
array_unshift($form["field"][$i]["options"], array("value" => " ", "text" => "Select a category ...", "depth" => 0, "selected" => "selected", "disabled" => "disabled"));