访问ZF2使用javascript创建了多选框

Accessing ZF2 created multiple select box using javascript

本文关键字:创建 ZF2 使用 javascript 访问      更新时间:2023-09-26

我使用zend框架2:在表单对象上创建了一个多选框

$contacts = new Element'Select('contacts');
$contacts->setLabel('All Contacts')
         ->setAttribute('name', 'contacts')
         ->setAttribute('multiple', 'multiple')
         ->setAttribute('size', 10)
         ->setOptions(array('options' => $users));

当按下表单上的按钮时,我想执行一些javascript:

$moveAllRight = new Element'Button('moveAllRight');
$moveAllRight->setLabel('Move All ->')
         ->setAttribute('value', 'Move All ->')
         ->setAttribute('onClick', 'moveAll(this.form.contacts,this.form.newContacts)');

不幸的是,当创建页面时,多选元素的名称会附加[]:

<select name="contacts[]" multiple="multiple" size="10">

我已经尝试在js函数调用中更改名称:

->setAttribute('onClick', 'moveAll(this.form.contacts[],this.form.newContacts[])');

但我仍然没有任何运气让它发挥作用。如果我从选择框中删除多选,它会起作用,但如果可能的话,我想使用多选框。有没有办法让它发挥作用?s

我意识到id也可以引用表单元素。我设置了一个id属性,其值与我尝试使用的名称相同:

$contacts = new Element'Select('contacts');
$contacts->setLabel('All Contacts')
         ->setAttribute('id', 'contacts')
         ->setAttribute('multiple', 'multiple')
         ->setAttribute('size', 10)
         ->setOptions(array('options' => $users));

元素在页面上创建:

<select name="contacts[]" id="contacts" multiple="multiple" size="10">

我现在可以像我最初想的那样引用它:

->setAttribute('onClick', 'moveAll(this.form.contacts,this.form.newContacts)');