将CF7用于wordpress,我想使用下拉菜单中选择的选项填充文本字段。预期的结果是,当我在“ oggetto”中选择一个特定值时,文本字段“ recipe”中将填充一个字符串。我不知道为什么这个简单的jquery脚本不起作用:
脚本:
<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("[email protected]");
document.getElementById("recipe").value = "[email protected]"
}
else {
jQuery('#recipe').val("[email protected]");
}
});
</script>
表单,其中“ 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>
我用过
document.getElementById("recipe").value
和
jQuery('#recipe').val("[email protected]");
经过许多尝试。但是函数内部所做的每个更改似乎都没有用,因为通过console.log ...代码执行似乎在change事件之前停止了。您对我的脚本中的问题有任何想法吗?
[确定,确定,确定。对于那些使用CF7遇到相同问题的人...问题是任何js代码都必须在格式化代码之后。所以...我的代码正确(都是document.getElementById(“ recipe”)。value和jQuery('#recipe')。val(“ [email protected]”);)。只需将脚本放在表单后,就可以了...