附加选项以使用xpath进行选择

时间:2014-03-31 09:44:48

标签: php xpath domdocument

我试图将选项附加到我从模板中获取的选项。

该技术本身工作正常,除了事实上只有最后一个迭代选项实际上在HTML中。因此,我不确定它是否实际附加或只是覆盖了我的选择模板中的所有内容。

$select = $this->elementTemplate->query('//select')->item(0);
foreach ($values as $value) {
    //Check to see post data is set, and if it is check if the value is the same. In case it is the option is 'selected'.
    if ($this->postData[$this->properties->get('name')] != "") {
        $selected = ($this->postData[$this->properties->get('name')] == $value) ? 'selected' : '';
    }

    $optionTemplate->setAttribute('value', $value);
    if ($selected == 'selected') {
        $optionTemplate->setAttribute('selected', $selected);
    }
    $optionTemplate->nodeValue = $value;
    $select->appendChild($optionTemplate);
}

//Last but not least re-fetch the div(container of the label and input) and set it
$div = $this->elementTemplate->query('//div')->item(0);
$this->setHTML($this->getInnerHTMLOfElement($div));

1 个答案:

答案 0 :(得分:0)

$optionTemplate未在任何地方定义,因此在循环中添加此内容应该可以修复它:

$optionTemplate = $doc->createElement('option');

(其中$doc是父文件)