简体   繁体   English

单击div元素,Ajax提交联系表单7表单

[英]Ajax submit contact form 7 form on click of div element

I'm using the Contact Form 7 plugin to run a simple form on my site, but I'd like to style the submit button to reflect the other buttons on my site. 我正在使用Contact Form 7插件在我的网站上运行一个简单的表单,但是我想设置“提交”按钮的样式以反映网站上的其他按钮。 Doing so requires the use of pseudo elements, which isn't possible with input elements. 这样做需要使用伪元素,而输入元素是不可能的。

I've adjusted the form markup to use a div as a submit button (below), but as it's just a default submit onClick, it refreshes the page. 我已经调整了表单标记,以将div用作提交按钮(如下所示),但由于它只是默认的提交onClick,因此会刷新页面。 Is there a way to hook this in to the CF7 AJAX submit rather than the default action so that executes the same way as if I were using the CF7 submit button (no page refresh)? 有没有一种方法可以将其连接到CF7 AJAX提交而不是默认操作,以便执行与使用CF7提交按钮相同的方式(不刷新页面)?

<div class="main-btn" onClick="document.forms['general-contact'].submit()">
 <span>Submit</span>
</div>

** general-contact is the name of the form to be submitted ** ** 一般联系方式是要提交的表格的名称**

There's a couple of ways - but usually just triggering the submit event for the form or click event on the submit button via jQuery are the simplest 有两种方法-但通常最简单的方法是触发表单的Submit事件或通过jQuery提交按钮上的click事件

$(document.forms['general-contact']).trigger( "submit" );

or triggering a click on the submit button: 或触发“提交”按钮的点击:

$(document.forms['general-contact']).find('input[type="submit"], input[type="image"]').trigger( "click" );

Should do the trick! 应该做的把戏!

By the way - if using in an onclick attribute you may need to change the $ to jQuery as WordPress loads jQuery in noconflict mode by default :) 顺便说一句-如果在onclick属性中使用,您可能需要将$更改为jQuery因为WordPress默认以noconflict模式加载jQuery :)

<div class="main-btn" onclick="jQuery(document.forms['general-contact']).trigger( "submit" );">
 <span>Submit</span>
</div>

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

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