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

拈花ヽ惹草 提交于 2019-12-25 02:24:52

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/4732615/set-selected-value-in-select-box-dropdown-list-based-on-first-letter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!