最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Run JavaScript on form submit in Drupal 7 - Stack Overflow

programmeradmin5浏览0评论

I have a form with a few fields the user should fill out. The form also have two hidden fields for the users current position (lat, long). I have spent quite some time now trying to find a way to fill these fields.

How I want it to work is like this: When form is submitted (jQuery OR OnSubmit in form tag) a JavaScript function that asks the device for location should be run. The hidden fields is then filled with the result.

I have working code for doing this but I can't get it to be called by Drupal.

Is there any way to run a JavaScript function upon form submit in Drupal or should I find another way to do it.

Many thanks,

I have a form with a few fields the user should fill out. The form also have two hidden fields for the users current position (lat, long). I have spent quite some time now trying to find a way to fill these fields.

How I want it to work is like this: When form is submitted (jQuery OR OnSubmit in form tag) a JavaScript function that asks the device for location should be run. The hidden fields is then filled with the result.

I have working code for doing this but I can't get it to be called by Drupal.

Is there any way to run a JavaScript function upon form submit in Drupal or should I find another way to do it.

Many thanks,

Share edited Nov 5, 2013 at 10:59 Muhammad Reda 27.1k14 gold badges98 silver badges106 bronze badges asked May 14, 2013 at 21:48 Fredrik JonssonFredrik Jonsson 611 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Try to add onclick event to the submit button using #attributes.

$form['SUBMIT_BUTTON']['#attributes'] = array(
    'onclick' => array("YourJsCallback()"),
);

OR If you already have something inside ['#attributes'] array, so you will need to add to it.

$form['SUBMIT_BUTTON']['#attributes']['onclick'] = array("YourJsCallback()");

Seem to have got this to work now. Added the following

$form['#attributes'] = array('OnSubmit' => 'myFunction();');

You have the following hook in "www/misc/ajax.js" that you can override in your javascript code:

/**
 * Modify form values prior to form submission.
*/
Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options)
 {
      // This function is left empty to make it simple to override for modules
      // that wish to add functionality here.
 };
发布评论

评论列表(0)

  1. 暂无评论