2010-11-10 65 views
1

我通過以下方式將onchange處理程序添加到所有cck字段。drupal選擇cck onchange處理程序

function bday_form_event_node_form_alter(&$form, &$form_state) { 
    $form['title']['#attributes'] = array('onchange' => "titleval()"); 
    $form['#after_build'][] = 'bday_form_event_node_form_cck_alter'; 
} 
function bday_form_event_node_form_cck_alter($form, &$form_state) { 
    $form['field_date1'][0]['value']['#attributes'] = array('onchange' => "dateval()"); //Text Field 
$form['field_city']['#attributes'] = array('onchange' => "cityval()"); //Select Field 
} 

但onChange處理爲選擇沒有添加到DOM。

+3

爲什麼在html中加入這個,而不是使用.js文件/ jQuery代替? – googletorp 2010-11-10 08:42:30

回答

2

googletorp說什麼建設一點點。

這不是行爲在Drupal添加到表單的方式。

有一個非常漂亮的JS api附帶的Drupal可以幫助你這樣做。

粗略你會想是這樣的。

Drupal.behaviors.myModuleBehavior = function(context) { 
    $('.title').change(function() {titleval() }) ; 
    $('.field_date1').change(function() {dateval()}); 
    $('.field_city').change(function() {cityval()}); 
}