$form['export'] = array(
'#type' => 'button',
'#value' => t('Export blank dealer'),
'#attributes' => array('onclick' => 'window.location=\'/admin/build/import-dealer/export-blank-dealer\';'),
);
发现总是不成功,试过alert('xxx'),可以实现,说明onclick事件成功了。
查看页面源码
<input type="submit" class="form-submit" onclick="window.location='/admin/build/import-dealer/export-blank-dealer';" value="Export blank dealer" id="edit-export" name="op">
明明是'#type' => 'button'怎么打出来的html是type="submit",
找下核心代码在form.inc里1937行找到theme_button函数
原来它把button的也当作submit处理,真不知道drupal的开发人员怎么想的。
原有代码:
/**
* Theme a form button.
*
* @ingroup themeable
*/
function theme_button($element) {
// Make sure not to overwrite classes.
if (isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
}
else {
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
}
return '<input type="submit" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ') .'id="'. $element['#id'] .'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
}
修正代码:
/**
* Theme a form button.
*
* @ingroup themeable
*/
function theme_button($element) {
// Make sure not to overwrite classes.
if (isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'form-' . $element['#button_type'] . ' ' . $element['#attributes']['class'];
} else {
$element['#attributes']['class'] = 'form-' . $element['#button_type'];
}
return '<input type="' . $element['#type'] . '" ' . (empty($element['#name']) ? '' : 'name="' . $element['#name'] . '" ') . 'id="' . $element['#id'] . '" value="' . check_plain($element['#value']) . '" ' . drupal_attributes($element['#attributes']) . " />\n";
}
很简单,就是把return部分的type="submit"改成type="' . $element['#type'] . '"
查看页面源码
<input type="button" class="form-submit" onclick="window.location='/admin/build/import-dealer/export-blank-dealer';" value="Export blank dealer" id="edit-export" name="op">
查看页面源码
<input type="button" class="form-submit" onclick="window.location='/admin/build/import-dealer/export-blank-dealer';" value="Export blank dealer" id="edit-export" name="op">
试试onclick,OK成功!
没有评论:
发表评论