$buttonstyle = " padding-left:6px; border-color:#ED6502; text-align:center; "; $fieldset = $form->addFieldset('steals_form', array('legend'=>Mage::helper('mode')->__('Create button'))); $fieldset->addField('runfile', 'button', array( 'name' => 'runfile', 'style'=> $buttonstyle,//定义按钮的css 'onclick'=>'runfile()',//js方法 'value' => 'Run Profile', ));点击按钮之后,运行js的runfile 方法,那么怎么将此方法加到这个form页面呢,办法不止一种,这里简单说一种;
$field = $fieldset->addField('runfilejs', 'hidden', array( 'name' => 'hiddenjs', )); $field->setRenderer($this->getLayout()->createBlock('mode/adminhtml_dect_edit_renderer_html'));上面代码创建一个hidden元素,然后在此元素引入一个renderer,在这个renderer文件里,就可以定义js方法了
class Company_Mode_Block_Adminhtml_Dect_Edit_Renderer_html extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface { public function render(Varien_Data_Form_Element_Abstract $element) { //You can write html here $html = "<script type='text/javascript'> function runfile(){ //写你的js代码在这里 } </script>"; return $html; } }
以上方法不见得是最完美的方法,但很容易理解。
其实还有一种更为简便的方法,一目了然,例子如下:
$addcomment->addField('rtype', 'radios', array( 'label' => $this->__('Select Type'), /* 'class' => 'validate-one-required-by-name', */ 'name' => 'rtype', 'required' => true, 'values' => array(array('label'=>'Masculino', 'value'=> 'M','class' => 'validate-genero'),array('label'=>'Feminino', 'value'=>'F')), /* double error message bug */ 'after_element_html' => '<script type="text/javascript">//js代码 </script>' ) );一个after_element_html就搞定!