简体   繁体   English

在提交表单时运行 JavaScript 代码

[英]Run JavaScript code on form submission

I want to run a JavaScript code upon a form being submitted preceding other default tasks that are carried out on form submit.我想在提交表单时运行 JavaScript 代码,然后再提交表单时执行的其他默认任务。

Can anyone suggest some approaches?任何人都可以提出一些方法吗?

For example, here is how you can execute some JS on Drupal's contact form before it is submitted using jQuery:例如,下面是在使用 jQuery 提交 Drupal 的联系表单之前如何执行一些 JS 的方法:

$('form#contact-mail-page input#edit-submit').click(function() { alert('do some stuff'); });

you can use this property of你可以使用这个属性

'#attributes' => array('onclick' => 'if(confirm('.$msg.')) return true; return false;'));

I am not sure if drupal has any special way to automate this or make it easier but in just normal HTML javascript you would do it like:我不确定 drupal 是否有任何特殊的方法可以自动执行此操作或使其更容易,但在正常的 HTML javascript 中,您会这样做:

<input type="submit" onClick="doStuff();" value="Submit">  //Note: you can use return(doStuff()); and if doStuff returns false then the form will not submit (great for checking if all fields are filled out correctly).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM