问题
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