Zend_form_element_select onchange in zend framework

和自甴很熟 提交于 2019-12-23 13:00:02

问题


I have a form named createDevice.php as:

class Admin_Form_CreateDevice extends Zend_Form
{
public function init()
{
    $this->setName('Create Device Access');
    $sort=new Zend_Form_Element_Select('employee_name');
    $sort->setLabel('Employee Name:');
    $this->addElements(array($sort));
    /* Form Elements & Other Definitions Here ... */
}

}

Now In controller action named viewDeviceAction() I have called this form as:

public function viewDeviceAction()
{
    echo 'viewDevice: ';
    $form_device=new Admin_Form_CreateDevice();
    $form_device->setMethod('post');
    $form_device->employee_name->addMultiOptions($aMembers);//here $aMembers is an array.
    $this->view->form=$form_device;
 }

Now I want following situation: On selecting any value from above dropdown a javascript function (which resides in viewDevice.phtml ) should be called.Like in general html as:

<select id="EmployeeId" onchange="loadDeviceId();">

So I just want to khow that how to implement the onchange event on the select element in zend framework


回答1:


That is possible to add in the server side itself. While creating your element, add the details for the onchange event like shown below.

$sort=new Zend_Form_Element_Select('employee_name',array('onchange' => 'loadDeviceId();'));

Now in your output HTML, you will see "onchange = 'loadDeviceId();'" attached to your select element.

Check my answer in an other question.




回答2:


Since you want to implement an event handler for the onChangeevent you will have to do it in javascript. There is no native way to implement it in PHP or Zend framework as far as I know.

Using JQuery you could do something like this:

$('#employee_name').change(function() 
{
   //call your javascript function here
});

You can even call your function directly like this:

$('#employee_name').change(yourFunctionName);

Hope that helps you.



来源:https://stackoverflow.com/questions/8645586/zend-form-element-select-onchange-in-zend-framework

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