Запуск массива строк внутри метода include? - PullRequest
0 голосов
/ 04 апреля 2019

Я создал массив с именем itemNameAreaReplace, содержащий регионы, которые мне нужно было найти.

Как использовать это в методе include ()?

Так что, если оно совпадает со словом 'UK', я могу сохранить его и заменить в каждом входе [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>

1 Ответ

0 голосов
/ 04 апреля 2019

Мне удалось создать цикл, который сканирует совпадающую строку в значении.

$(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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...