首页 > 解决方案 > JS修改基于URL填充联系表单下拉列表

问题描述

在构建网站时,我尝试使用 CF7 的动态文本插件来填充基于先前站点的字段。

但是,虽然它显示了所需的 URL(在加载联系页面后从提议传递到联系页面“www.webpage.com/contact?offer1”,但下拉菜单不会显示想要的选项。

基本上

在报价页面上时 > 按“立即联系”按钮 > 加载带有预归档报价字段的 CF7。

使用 Calluna 主题和 WP 4.9.9

我曾尝试在 wordpress 中使用动态选择扩展名,并且我使用了简码进行选择,但它不起作用。用“if if else”语句替换它是完美的。像这样的东西可以吗?

$(document).ready(function () {
    if(window.location.href.contains("?offer1") -1) {
       select("offer1");
    }

    if else(window.location.href.contains("?offer2") -1) {
       select("offer2");
    }    

    if else(window.location.href.contains("?offer3") -1) {
       select("offer2");
    }  

    if else(window.location.href.contains("?offer4") -1) {
       select("offer2");
    }

    else{
       select("Angebote");
    } 

 });

标签: javascriptwordpress

解决方案


这不是很好的方式。使用以下一个;

$(document).ready(function () {

  var matchedData = window.location.href.match(/\?(offer(\d))/);

  if (matchedData) {
    select(matchedData[1]);
    return;
  }

  // default behaviour
   select("Angebote");
 });

.. 需要映射操作的情况:

$(document).ready(function () {

  var matchedData = window.location.href.match(/\?(offer(\d))/);
  var actionsMap = new Map([["offer3", "offer2"], ["offer4", "offer2"]])

  if (matchedData) {
    var actionName = actionsMap.get(matchedData[1]) || matchedData[1];
    select(actionName);
    return;
  }

  // default behaviour
  select("Angebote");
 });

推荐阅读