Zend_form_element_select onchange in zend framewor

2019-06-27 10:21发布

问题:

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.