如何将javascript文件添加到自定义表单并将数据传递到js文件
我正在使用Drupal8,我有定制的ajax表单,当我使用[ajax HTMLCommand]测试它时,它工作得很好。但现在我想将数据发送到javascript文件,以在div中显示它,并执行其他一些操作。 我所做的是在如何将javascript文件添加到自定义表单并将数据传递到js文件,javascript,forms,drupal,Javascript,Forms,Drupal,我正在使用Drupal8,我有定制的ajax表单,当我使用[ajax HTMLCommand]测试它时,它工作得很好。但现在我想将数据发送到javascript文件,以在div中显示它,并执行其他一些操作。 我所做的是在themes/custom/myform/assets/js/simple form.js下创建js文件,但单击元素时控制台中没有显示任何内容 Drupal.behaviors.simple_form = function(context) { $('.btn-primary')
themes/custom/myform/assets/js/simple form.js
下创建js文件,但单击元素时控制台中没有显示任何内容
Drupal.behaviors.simple_form = function(context) {
$('.btn-primary').click(function() {
console.log('clicked');
});
};
并将其添加到themes/custom/myform/myform.libraries.yml
simple-form-js:
js:
assets/js/simple-form.js: {}
我的自定义表格
modules/custom/my_module/src/Form/SimpleForm.php
<?php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
/**
* Our simple form class.
*/
class SimpleForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'my_module';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
//$form = parent::buildForm($form, $form_state);
$form['#theme'] = 'simple_form';
$form['massage'] = [
'#type' => 'markup',
'#markup' => '<div class="result_message"></div>',
];
$form['number_1'] = [
'#type' => 'textfield',
'#title' => $this->t('number 1'),
'#attributes' => ['class' => ['form-control ml-sm-2 w-100']],
'#required' => TRUE,
];
$form['number_2'] = [
'#type' => 'textfield',
'#title' => $this->t('number one'),
'#attributes' => ['class' => ['form-control ml-sm-2 w-100']],
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'button',
'#value' => $this->t('Calculate'),
'#attributes' => ['onclick'=>'return false;','class' => ['mt-3 btn btn-primary btn-me2']],
'#attached' => [
'library'=>[
'simple-form',
]
],
'#ajax' => [
'callback' => '::setMessage',
]
];
return $form;
}
public function setMessage(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$letter = $form_state->getValue('number_1');
$code = $form_state->getValue('number_2');
$fetchUrl = 'http://example.com/api';
$responseAPI = \Drupal::httpClient()->get($fetchUrl, array('headers' => array('Accept' => 'text/plain')));
$data = (string) $responseAPI->getBody();
//drupal_set_message($data);
$response->addCommand(
new HtmlCommand(
'.result_message',
'<div class="my_top_message">' . $this->t('The result is @result', ['@result' => ($data)])
)
);
return $response;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
您可以使用
HOOK\u form\u alter
附加库
*/**
* Implements hook_form_alter().
*/
function my_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if($form_id == "my_module") {
$form['#attached']['library'][] = 'myform/simple-form-js';
$form['#attached']['library'][] = 'myform/simple-form-css';
$form['#attached']['drupalSettings']['my_module']['variable'] = 'Your Custom values to JS';
return $form;
}
}
您需要的是ajaxCommand,而不是行为。 创建自定义ajax命令,并在创建ajax响应时将变量传递给它 这是帮助
并从Drupal Cores InvokeCommand如何附加库中获得灵感,您不需要主题或模块文件,例如:
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
足够在您的案例中附加一个库。您应该调用js文件,如下所示:
$form['#attached']['library'][] = 'simple-form-js';
然后,库下的文件将与表单一起加载
$form['#attached']['library'][] = 'simple-form-js';