2012-09-05 30 views
0

我正在使用zend表單來創建表單。我也爲JavaScript使用mootools。將onclick事件應用於選項

$this->addElement('radio', 'alone', array(
    'label' => 'Are you going to be taking part with anyone else?', 
    'required' => true, 
    'onClick' => 'showFields();', 
    'multiOptions' => array(
     'yes' => 'Yes', 
     'no' => 'No' 
    )) 
); 

目前,如果選擇任何選項,onclick事件工作。我如何才能讓它只爲被選中而工作?

+0

讓你'showFields的第一行()'函數驗證'yes'選擇在做其他事情之前 –

回答

1

你可以試試這個...

$this->addElement('radio', 'alone', array(
    'label' => 'Are you going to be taking part with anyone else?', 
    'required' => true, 
    'onClick' => 'showFields(this);', 
    'multiOptions' => array(
     'yes' => 'Yes', 
     'no' => 'No' 
    )) 
); 

而且在你的函數...

function showFields(elem) 
{ 
    if(elem.value != 'yes') 
     return false; 

    // rest of the code 
} 
+0

當嘗試這個解決方案時,我得到這個錯誤:'未捕獲的類型錯誤:無法讀取未定義的屬性'值' – RSM

+0

你有沒有改變你的控制器代碼? –

+0

通過使用id而不是value來排序該問題。但是,當頁面加載顯示隱藏的字段時。當我點擊不,但他們走了 – RSM