gpt4 book ai didi

javascript - 如何避免使用 OnChange 和 Marketo 加载表单

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:32 26 4
gpt4 key购买 nike

我有一个带有“onchange”功能的下拉列表,用于根据用户输入加载 Marketo 表单。表单加载正确,除了当用户最初选择“需要新产品”然后切换到“需要支持”(或反之亦然)时,“产品表单”不会消失。取而代之的是,“产品表格”保留下来,另外加载“需要支持”表格(在产品表格下方)。用户可以多次切换/触发,从而产生许多额外的形式,这是一个问题!

我需要帮助调整代码,以便始终只加载一个表单,并且当用户触发器时,用户选择的先前表单不再存在。有帮助吗??谢谢!

<script src="//app-e.marketo.com/js/forms2/js/forms2.js"></script>

<p>How can we help?</p>
<select id="mySelect" onchange="myFunction()">
<option value="default">--please select a reason--</option>
<option value="product">Need a new product</option>
<option value="support">Need support</option>
</select>


<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
if (x == "product") {
MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber", *uniquenumber);
}
else if (x == "support") {
MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
}
}
</script>

<form id="mktoForm_*uniquenumber1"></form>
<form id="mktoForm_*uniquenumber2"></form>

最佳答案

我推荐以下内容:

  1. 实现一种检查 Marketo 表单是否已加载的方法。例如:

    //Checks for element that matches "mktoForm_". If match, variable returns id of element. If no match, variable returns undefined. 
    var y = document.querySelector('[id^=mktoForm_]').id;
  2. 实现一种删除 Marketo 表单的方法。例如:

    //jQuery implementation to remove form element
    $( "form" ).remove();
  3. 实现条件逻辑以确定当前状态。例如:

    if (x == "product" && y === undefined) {
    MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber", *uniquenumber);
    }
    else if (x == "product" && y != undefined) {
    //jQuery implementation to remove form element
    $( "form" ).remove();
    MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
    }
    else if (x == "support" && y === undefined) {
    MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
    }
    else (x == "support" && y != undefined) {
    //jQuery implementation to remove form element
    $( "form" ).remove();
    MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
    }

这是一个可能的实现:

<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
//Checks for element that matches "mktoForm_". If no match, variable returns undefined.
var y = document.querySelector('[id^=mktoForm_]').id;
document.getElementById("demo").innerHTML = "You selected: " + x;

if (x == "product" && y === undefined) {
MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber", *uniquenumber);
}
else if (x == "product" && y != undefined) {
//jQuery implementation to remove form element
$( "form" ).remove();
MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
}
else if (x == "support" && y === undefined) {
MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
}
else (x == "support" && y != undefined) {
//jQuery implementation to remove form element
$( "form" ).remove();
MktoForms2.loadForm("//app-e.marketo.com", "*uniquenumber2", *uniquenumber2);
}
}
</script>

关于javascript - 如何避免使用 OnChange 和 Marketo 加载表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26652717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com