2014-11-05 76 views
0

我嘗試在選擇元素的onchange上添加一個表單輸入Elemnt。 我有這樣的代碼:zend addElements form onchange select

$this->addElement('select', 'nationality', array(
     'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'), 
     'required' => true, 
     'label' => 'Nation', 
     'filters' => array('StringTrim'), 
     'onChange' => '???' 
    )); 

我嘗試粘貼我的新元素,而不是「???」

$this->addElement('select', 'nationality', array(
     'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'), 
     'required' => true, 
     'label' => 'Nation', 
     'filters' => array('StringTrim'), 
     'onChange' => '$this->addElement(
         'text', 'codice_fiscale', array(
         'required' => true, 
         'label' => 'Codice fiscale', 
         'required' => true 
     ))' 
    )); 

我知道是不正確的,但我沒有找到任何有關它的文檔。 如何在change選擇值上添加元素?

在此先感謝

回答

0

我會處理這個問題以不同的方式,如果有可能爲你: JavaScript函數對的onChange調用。這將是語法正確的(正如你所說,你寫的是不正確的,它將被完全打印出onChange屬性,而不是通過php解釋)。

所以,我會寫

'onChange' => 'addElement()' 

然後聲明的JavaScript功能,處理新的元素。或者,您可以在之前準備好元素(如果您有固定號碼),並顯示/隱藏它們。如果你沒有太多元素,這可能會很方便,但如果它們太多了,只需通過javascript添加需要的元素即可。

僞代碼的Javascript

function addElement() 
{ 
    // add your element here, or show/hide it 
} 

據我從我的經驗,類似的案件

Zend_form_element_select onchange in zend framework

Zend Form: onchange select load another view content

看見你就不會從Javascript,如果你逃跑想要處理「onChange」

+0

是的我嘗試,但我的想法是在Zend平臺中聲明其他字段並通過選擇的onchange來控制這個。這種方法的想法? – 2014-11-05 15:32:00

+0

據我所知,你將需要JavaScript(例如jQuery)來處理onChange事件。 (http://stackoverflow.com/questions/13126894/zend-form-onchange-select-load-another-view-content)所以,我會添加你想要的所有元素,將它們設置爲禁用/不可見,然後使用JavaScript函數只是爲了顯示/啓用你想要的 – Gounemond 2014-11-05 16:11:51

+0

好的非常感謝@Gounemond。 Potevamo parlare in italiano信條^ __ ^ – 2014-11-06 09:59:46