开发者

Set 'selected' value in select box dropdown list - based on first letter

I have a zend form that has a select box with 1000+ id->name options sorted alphabetically. When rendered and looking at it in a browser,开发者_运维百科 if you type Ch it goes all the way to that option;

Is there a way I can set the value to be selected via the first few letters after the form has been initialized? In other words $form->getElement('name')->setSelected('Ch') or similar;

I know that with setValue(34) I can set the name to be selected that has the ID 34.


Just wrote the code myself

class My_Form_Element_Select extends Zend_Form_Element_Select{
/**
 * Sets the the first option to start with certain letters to be selected
 * @param string $string The first few letters to search for
 */
public function setSelected($string){
    $string = strtolower($string);
    $options = $this->_getMultiOptions();
    $length = strlen($string);
    foreach($options as $value => $option){ 
        if($string == strtolower(substr($option,0,$length))){
            $this->setValue($value);
            break;  
        }
    }
    return $this;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜