单击按钮时,我希望在 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
发布评论
评论(2)
表单 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).
看起来最简单的解决方案是,因为您实际上不需要按钮来提交和其他表单内容。
添加文本链接
使用 css .mybuttons 将链接样式设置为按钮{}
将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.
add a link to the text
style the the link as button using css .mybuttons{}
hook the js on the id. $(#mybutton1).alert..