简体   繁体   English

提交联系表格7后更改Div的背景

[英]Change Background Of Div Once Contact Form 7 Is Submitted

So I'm trying to add a class to the container (.right-side-product-page) and the h2 on a contact 7 form. 因此,我试图将一个类添加到容器(.right-side-product-page)和Contact 7表单上的h2上。 Here's a link: 这里是一个链接:

https://nameplicity.com/domains/miningaid/ https://nameplicity.com/domains/miningaid/

The goal is to change the class so the blue background and gray background become white, but only after an offer is submitted. 目标是更改班级,以便蓝色背景和灰色背景变为白色,但仅在提交报价之后。

I've tried to add CSS and JavaScript, but can't seem to get anything working. 我尝试添加CSS和JavaScript,但似乎无法正常工作。 Here is the code I've tried to use in the "Additional Settings" section under the Contact Form 7 plugin: 这是我尝试在Contact Form 7插件下的“其他设置”部分中使用的代码:

document.addEventListener( 'wpcf7submit', function( event ) {
if ( '19533' == event.detail.contactFormId ) {
var theDropDown = document.querySelector(".right-side-product-page");
theDropDown.classList.add("MyClass");
}, false );

Could anyone provide direction as to what I'm doing wrong? 谁能提供我做错事情的方向?

There's an error in your code: you're missing one curly bracket in there. 您的代码有错误:您在其中缺少一个花括号。

Try this: 尝试这个:

<script>
document.addEventListener( 'wpcf7submit', function( event ) {
    if ( '19533' == event.detail.contactFormId ) {
        var theDropDown = document.querySelector(".right-side-product-page");
        theDropDown.classList.add("MyClass");
    }
}, false );
</script>

Aside from the error which was mentioned above, you can further complete your solution based on the class added to the wpcp7-response-output block, upon successfully sending the message, the wpcf7-mail-sent-ok class is added. 除了上述错误外,您还可以根据添加到wpcp7-response-output块中的类进一步完善解决方案,成功发送消息后,将添加wpcf7-mail-sent-ok类。 Knowing this, we can utilize these classes with a check, here's an example: 知道了这一点,我们可以对这些类进行检查,这是一个示例:

$( document ).ready(function() {
    var outputBlock = $(".wpcf7-response-output");
    var theDropDown = document.querySelector(".right-side-product-page");
    $( ".wpcf7-submit" ).click(function() {
        //Start an interval check after submit has been clicked
        var intervalCheck = setInterval(function () {
            if (outputBlock.hasClass("wpcf7-mail-sent-ok")) {
                // The form has been submitted successfully, set the class to the block to change color
                theDropDown.classList.add("MyClass");
                // Stop running the interval checker after class has been added
                clearInterval(intervalCheck);
            }
        },1000);
    });
});

Try: 尝试:

document.querySelector('.wpcf7-form').addEventListener('submit', function (ev) {
    if(this['_wpcf7'].value == '19533') {
        document.querySelector(".right-side-product-page").classList.add("MyClass");
    }
});

or 要么

document.querySelector('.wpcf7-form').addEventListener('wpcf7submit', function (ev) {
    if(this['_wpcf7'].value == '19533') {
        document.querySelector(".right-side-product-page").classList.add("MyClass");
    }
}, false);

One of them should work. 其中之一应该起作用。 I believe the issue is that the event wpcf7submit you're listening to does not exist on document. 我认为问题在于文档中不存在您正在监听的wpcf7submit事件。 It exists on document.querySelector('.wpcf7-form'). 它存在于document.querySelector('。wpcf7-form')上。

Got it! 得到它了!

Had to put it right under the submit button and encase it in tags. 必须将其放在“提交”按钮下,并用标签括起来。 Thank you everyone for your help!!! 谢谢你们每一个人的帮助!!!

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

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