从drupal调用JS文件中的函数

时间:2015-01-12 02:30:46

标签: javascript drupal drupal-7 drupal-modules

我在模块中包含了一个javascript" myid"我使用这段代码开发drupal:

function myid_init() {
    drupal_add_js(drupal_get_path("module", "myid") . "js/myid.js");   
}

这是我的Javascript文件" myid.js":

function myid_js_start(){
    alert("hello world");
}

以下是创建调用警报功能的按钮的代码:

$form['add_button'] = array(
    '#type' => 'button',
    '#value' => 'Take a picture',
    '#attributes' => array('onclick' => 'myid_js_start()'),    
);

我不知道哪里出错了。该按钮显示但不会触发任何警报功能。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

如果你已经遵循了正确的drupal目录结构,并且一切都在它应该的位置,那么你可能正在将属性[onclick]设置为'myid_js_start()'。尝试像

一样
$form['add_button'] = array(
  '#type' => 'button',
  '#value' => 'Take a picture',
  '#attributes' => array('onclick' => 'myid_js_start'),    
);

也许这会解决它。

我目前没有设置drupal系统来测试你的代码。它可能是别的,但值得一试。

答案 1 :(得分:0)

使用#attach.js文件附加到表单本身。

$form['#attached']['js'] = array(
    drupal_add_js(drupal_get_path("module", "myid") . "js/myid.js"),
);
相关问题