将按钮添加到 Webform - 问题:执行提交,而不是 Ajax 回调
我使用 hook_form_alter 向 Web 表单添加一个按钮:
$form['submit_ajaxSearch'] = array(
'#type' => 'button',
'#ajax' => array(
'action' => 'click',
'callback' => 'search_callback',
'wrapper' => 'confirm',
'method' => 'replace',
'name' => 'search',
),
'#value' => t('Address Lookup'),
);
我可以在模块中设置 jQuery .click(),但无法执行 Ajax 回调。当按钮作为模块的一部分添加到表单时(即,如果它是 mymodule_form
),它会起作用,但是当添加到 mymodule_form_alter
中的 Web 表单时,它会执行提交而不是回调。
如何获取 ajax 回调来执行 Ajax,而不是 Submit?
I am adding a button to a webform using hook_form_alter:
$form['submit_ajaxSearch'] = array(
'#type' => 'button',
'#ajax' => array(
'action' => 'click',
'callback' => 'search_callback',
'wrapper' => 'confirm',
'method' => 'replace',
'name' => 'search',
),
'#value' => t('Address Lookup'),
);
I can setup a jQuery .click() in the module, but can't get the Ajax callback to execute. It works when the button is being added to a form as part of the module (i.e. if it was mymodule_form
), but when added to a webform in mymodule_form_alter
it is executing a submit instead of the callback.
How can I get the ajax callback to execute the Ajax, not Submit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于即使您只想要一个简单的按钮,Drupal 也会创建一个“提交”按钮,因此您必须向 drupal 指定您不希望该按钮执行提交回调。
您可以通过将该按钮的“#executes_submit_callback”设置为 false 来完成此操作。
例如:
Since Drupal creates a "Submit" button even though you only wanted a simple button, you will have to specify to drupal that you don't want the button to execute the submit callback.
You can do this by setting "#executes_submit_callback" to false for that button.
eg:
正如 drupal 文档所述,“#executes_submit_callback”不会阻止页面发送 POST 请求。
为此,您必须添加自定义属性,例如:
As drupal doc states it, "#executes_submit_callback" won't prevent the page to send a POST request.
To do so, you have to add a custom attribute like: