简体   繁体   English

JQuery根据联系表7中的选择值自动填充文本字段

[英]JQuery automatically fill textfield based on select values in Contact Form 7

Using CF7 for wordpress, I want to populate a textfield using the option selected in a dropdown.将 CF7 用于 wordpress,我想使用下拉列表中选择的选项填充文本字段。 The expected result is that when I select a specific value in "oggetto" then the textfield "recipe" is populated with a string.预期的结果是,当我在“oggetto”中选择特定值时,文本字段“recipe”将填充一个字符串。 I cannot figure out why this simple jquery script doesnt work:我无法弄清楚为什么这个简单的 jquery 脚本不起作用:

The script:剧本:

        <script text="text/javascript">
        console.log("Inside the script");
        var valore; 
        jQuery('#oggetto').change(function(){
        console.log("I'm inside");
        valore = jQuery('#oggetto').val();
        console.log(valore);
        if (valore == "Corsi") {
        console.log("Now I know what to do");
        //jQuery('#recipe').val("corsi@mydomain.com");
        document.getElementById("recipe").value = "corsi@mydomain.com"
        }
        else  {
        jQuery('#recipe').val("everybody@mydomain.com");
        }
        });
        </script>

The form, where "id:" defines both the fields' ids involved:表单,其中“id:”定义了涉及的两个字段的id:

<div class="row">
<div class="col-sm-6">[text* your-name placeholder "Nome e cognome *"]</div>
<div class="col-sm-6">[email* email placeholder "Email *"]</div>
<div class="col-sm-6">[tel phone "Telefono"]</div>
<div class="col-sm-12">[email* destinatario id:recipe]</div>
<div class="col-sm-6">[select* subject id:oggetto "Oggetto della richiesta *" "Corsi" "Informazioni generali" "Informazioni sugli eventi" "Partnership" "Altro"]</div>
<div class="col-xs-12">[textarea* message placeholder "Messaggio *"]


[submit class:btn "Invia"]</div><div class="col-xs-12"></div></div>

I used我用了

document.getElementById("recipe").value

and

jQuery('#recipe').val("everybody@mydomain.com");

just after many many attempt.只是经过多次尝试。 But every change done inside the function seems useless because by the console.log... code execution seems to stop before the change event.但是在函数内部所做的每个更改似乎都没用,因为通过 console.log... 代码执行似乎在更改事件之前停止。 Any idea on what's wrong in my script?知道我的脚本有什么问题吗?

Ok, ok, ok.好好好。 For those who get the same problem with CF7... the problem is that any js code must be AFTER the formatting code.对于那些在 CF7 中遇到同样问题的人......问题是任何 js 代码都必须在格式化代码之后。 So... my code i right (both document.getElementById("recipe").value and jQuery('#recipe').val("everybody@mydomain.com"); ).所以......我的代码是正确的(document.getElementById("recipe").value 和 jQuery('#recipe').val("everybody@mydomain.com"); )。 Simply put the script after the form, this way...只需将脚本放在表单之后,这样...

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

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