在 drupal 的 .module 文件中包含 javascript 函数

发布于 2024-09-07 10:40:39 字数 1267 浏览 1 评论 0 原文

单击按钮时,我希望在 drupal 中使用 javascript 显示 hi 消息。我已经制作了一个 .js 文件,并且知道要包含我必须使用 drupal_add_js(drupal_get_path('module', 'document') .'/click.js ');但问题是创建按钮我使用 $form['click'] = array( '#type'=> '按钮', '#attributes'=>; array('onclick' =>drupal_add_js(drupal_get_path('module', 'document') . '/cancel.js')), '#value'=> t('点击'), );

我希望在单击按钮时显示我包含在 js 文件中的 hi 消息。 请帮助


嗨,感谢您的关心............ 中进行的方式

这是我在 .module 文件function document_form(&$node) { $form['点击'] = 数组( '#type'=> '按钮', '#attributes'=>;数组('onclick' => message()), '#value'=> t('点击'), ); }

函数 document_form_alter(&$form, &$form_state, $form_id) { drupal_add_js(drupal_get_path('模块', '文档').'/cancel.js', '模块'); $settings['点击'] = 数组( 'nid'=> $form['nid']['#value'], 'cid'=> $form['cid']['#value'], 'uid'=> $form['uid']['#value'], 'pid'=> $form['pid']['#value'], ); drupal_add_js($settings, '设置'); 我的

.js文件代码如下:

函数消息() { alert("此警报框已被调用"); }

<body>
</body>

但仍然单击按钮,我没有收到消息“此警报框已被调用” 请帮忙现在问题出在哪里............ 提前谢谢.... 等待您的回复

on click of button i want hi message to be displayed using javascript in drupal.I have made a .js file and know that to incude that i must use drupal_add_js(drupal_get_path('module', 'document') .'/click.js'); but the problem is to create button i used $form['click'] = array(
'#type' => 'button',
'#attributes' => array('onclick' =>drupal_add_js(drupal_get_path('module', 'document') . '/cancel.js')),
'#value' => t('click'),
);

I want that hi message which i have included in js file to be shown when button is clicked.
Please help


Hi thanx for your concern..........
here is the way i proceeded in .module file

function document_form(&$node) {
$form['click'] = array(
'#type' => 'button',
'#attributes' => array('onclick' =>message()),
'#value' => t('click'),
);
}

function document_form_alter(&$form, &$form_state, $form_id) {
drupal_add_js(drupal_get_path('module', 'document').'/cancel.js', 'module');
$settings['click'] = array(
'nid' => $form['nid']['#value'],
'cid' => $form['cid']['#value'],
'uid' => $form['uid']['#value'],
'pid' => $form['pid']['#value'],
);
drupal_add_js($settings, 'setting');
}

and my .js file code is as follows:

function message()
{
alert("This alert box was called");
}

<body>
</body>

but still onclick of button i m not getting the message "This alert box was called"
Kindly help where the problem is coming now.......
Thanx in advance....
in wait of your response

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-09-14 10:40:39

表单 alter 不会按照您想要的方式添加 JS 文件。

在创建表单的函数中,您可以在创建表单数组之外使用 drupal_add_js。

然后你可以使用onclick来调用你的JS文件中的函数。

更好的方法是使用 drupal 行为向按钮添加点击监听器(参见示例 此处)。

The form alter won't add the JS file in the way you are wanting.

In the function you create the form you can use drupal_add_js, outside of the creation of the form array.

Then you can use the onclick to call the function in your JS file.

A better way to do this is to use drupal behaviours to add an click listner to the button (see example here).

魂ガ小子 2024-09-14 10:40:39

看起来最简单的解决方案是,因为您实际上不需要按钮来提交和其他表单内容。

  1. 添加文本链接

  2. 使用 css .mybuttons 将链接样式设置为按钮{}

  3. 将js挂在id上。 $(#mybutton1).alert..

Looks like simplest solution would be, as you actually don't need the button to go to submit and other form stuff.

  1. add a link to the text

  2. style the the link as button using css .mybuttons{}

  3. hook the js on the id. $(#mybutton1).alert..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文