【问题标题】:Running a string array inside the include method?在 include 方法中运行字符串数组?
【发布时间】:2019-08-26 04:51:40
【问题描述】:

我创建了一个名为 itemNameAreaReplace 的数组,其中包含我需要查找的区域。

如何在 include() 方法中使用它?

因此,如果它与单词“UK”匹配,我可以将其存储并在每个 input[name=item_name] 中将其替换为“Europe”或其他内容。

const itemName = $(".postal input[name=item_name]");
const itemNameAreaReplace = ['UK', 'EUROPE', 'AMERICA', 'REST OF WORLD'];

console.log(itemName.val());

$("#selectArea").change(function() {

  const selectedArea = $(this).val();

  const replace = () => {
    if (itemName.val().includes('UK')) {
      console.log("Replacing UK with Europe...");
      itemName.val(itemName.val().replace('UK', 'Europe'));
      console.log(itemName.val());
    } else {
      console.log("Replacing Europe with UK...");
      itemName.val(itemName.val().replace('Europe', 'UK'));
      console.log(itemName.val());
    }
  };
  
  replace();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="selectArea" name="selectArea">
  <option value="post-uk" selected="selected">UK</option>
  <option value="post-eu">Europe</option>
  <option value="post-am">USA, South America and Canada</option>
  <option value="post-rw">Rest of World - Asia, Japan, Australia</option>
</select>

<form class="postal 0" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
  <!-- Change UK -->
  <input type="hidden" name="item_name" value="(ABC) - UK - Product" />
</form>

【问题讨论】:

  • 你能把你的问题的简短版本带来吗?我喜欢帮忙,但是阅读时间太长了
  • 如上面的评论 - 您的大部分代码似乎与问题无关(隐藏的电子邮件字段?),更多信息在这里minimal reproducible example
  • 对不起,我基本上是在尝试替换 item_name 的值,但只有一个单词。例如,如果所选选项显示日本,则 item_name 将显示“ABC - 日本 - 产品”,而不是“ABC - 英国 - 产品”。
  • 我已经编辑了帖子。真的很抱歉,我不是一个很好的沟通者。
  • “我如何在 include() 方法中使用它” - 简短的回答:你不需要。 string.include() 只会使用另一个字符串。您可以选择从简单循环到itemNameAreaReplace,再到将数组转换为正则表达式;或过度设计的过程,例如将文本按空格拆分为数组并进行连接。

标签: javascript jquery html arrays dom


【解决方案1】:

我能够创建一个循环来扫描值中的匹配字符串。

$(document).ready(function() {

  // Searches for the input in every form.
  const itemName = $(".postal input[name=item_name]");

  // Array of regions.
  const getShippingRegion = ['UK', 'EUROPE', 'AMERICA', 'JAPAN', 'REST OF WORLD'];

  // Stores new region string.
  let shippingRegion;

  // Log the current value of itemName.
  console.log(itemName.val());

  // Detects when the selected option's change.
  $("#selectArea").change(function() {

    // Stores the current option's value.
    const selectedArea = $(this).val();

    // If-Else - Gives shippingRegion its new string.
    switch (selectedArea) {
      case 'post-uk':
        shippingRegion = 'UK';
        break;
      case 'post-eu':
        shippingRegion = 'EUROPE'
        break;
      case 'post-am':
        shippingRegion = 'AMERICA'
        break;
      case 'post-jp':
        shippingRegion = 'JAPAN'
        break;
      case 'post-rw':
        shippingRegion = 'REST OF WORLD'
        break;
      default:
        console.log('Error');
    }

    // Function - Replaces the input's value based on the selected option.
    const replace = () => {

      // Checks each string in the array.
      for (let i = [0]; i < getShippingRegion.length; i++) {

        // If the input's value finds any of the string from the array...
        if (itemName.val().includes(getShippingRegion[i])) {

          // ... replace the selected string from shippingRegion.
          itemName.val(itemName.val().replace(getShippingRegion[i], shippingRegion));

        }

      }

    };

    // Run the function above.
    replace();

    // Log the new value.
    console.log(itemName.val());

  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-wrapper">

  <span><strong>Please select your area below:</strong></span>

  <div class="form-group">
    <select class="form-control" id="selectArea" name="selectArea">
      <option value="post-uk" selected="selected">UK</option>
      <option value="post-eu">Europe</option>
      <option value="post-am">USA, South America and Canada</option>
      <option value="post-jp">Japan</option>
      <option value="post-rw">Rest of World - Asia, Japan, Australia</option>
    </select>
  </div>

  <form class="postal 0" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="item_name" value="(ABC) - UK - Product Name" />
  </form>

</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    相关资源
    最近更新 更多