链接状态列表将html列入特定页面



我在我的主页上添加了一个下拉列表列表,该列表使用WordPress/Elenntor。

从下拉列表中选择项目时,我想将用户重定向到另一个URL。例如,当选择佐治亚州时,我想重定向到此URL:https://www.everlastingopportunities.com/georgia/

当佐治亚州选中乔治亚州时,我该如何使页面转到上述URL?

<select>
	<option value="AL">Alabama</option>
	<option value="AK">Alaska</option>
	<option value="AZ">Arizona</option>
	<option value="AR">Arkansas</option>
	<option value="CA">California</option>
	<option value="CO">Colorado</option>
	<option value="CT">Connecticut</option>
	<option value="DE">Delaware</option>
	<option value="DC">District Of Columbia</option>
	<option value="FL">Florida</option>
	<option value="GA">Georgia</option>
	<option value="HI">Hawaii</option>
    <!-- ... list continues ... -->
</select>				

您可以通过将所选选项传递给JavaScript函数并使用它重定向到新URL来实现它。

理想是在顶部添加空白选项,以便希望阿拉巴马州作为选项的人做出一次选择。

我已经根据您在问题中显示的可见名称制作了脚本。

function my_function(event) {
  var options = event.target.options;
  var selected = options[options.selectedIndex].text;
  window.location.href = 'https://www.everlastingopportunities.com/' + selected + '/';
}
<select onchange="my_function(event)">
	<option value="">---</option>
	<option value="AL">Alabama</option>
	<option value="AK">Alaska</option>
	<option value="AZ">Arizona</option>
	<option value="AR">Arkansas</option>
	<option value="CA">California</option>
	<option value="CO">Colorado</option>
	<option value="CT">Connecticut</option>
	<option value="DE">Delaware</option>
	<option value="DC">District Of Columbia</option>
	<option value="FL">Florida</option>
	<option value="GA">Georgia</option>
	<option value="HI">Hawaii</option>
    <!-- ... list continues ... -->
</select>

您可以轻松使用jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="selectCountry">
	<option value="AL">Alabama</option>
	<option value="AK">Alaska</option>
    <!-- ... list continues ... -->
</select>
<script type="text/javascript">
$( document ).ready(function() {
  $("#selectCountry").change(function(){
    window.location.href = "https://www.everlastingopportunities.com/" + $("#selectCountry > option:selected").text() + "/";
  });
});
</script>

从上面的帖子中复制和过去的代码,以获取完整代码,然后输入网站链接。

<select onchange="location = this.options[this.selectedIndex].value;">
    <option>Please select</option>
    <option value="everlastingopportunities.com/Alabama/">Alabama (AL)</option>
    <option value="(ENTER WEBSITE LINK HERE)">Alaska (AK)</option>
    <option value="(ENTERWEBSITE LINK HERE)">Arizona (AZ)</option>
<div class="col-md-4">
        <label f`enter code here`or="inputState" class="form-label">State</label>
        <select id="inputState" class="form-select">
          <option selected>Choose...</option>
          <option>
            <!-- option -->
          <option value="---">---</option>
          <option value="Alabama">Alabama</option>
          <option value="Alaska">Alaska</option>
          <option value="Arizona">Arizona</option>
          <option value="Arkansas">Arkansas</option>
          <option value="California">California</option>
          <option value="Colorado">Colorado</option>
          <option value="Connecticut">Connecticut</option>
          <option value="Delaware">Delaware</option>
          <option value="District of Columbia">District of Columbia</option>
          <option value="Florida">Florida</option>
          <option value="Georgia">Georgia</option>
          <option value="Guam">Guam</option>
          <option value="Hawaii">Hawaii</option>
          <option value="Idaho">Idaho</option>
          <option value="Illinois">Illinois</option>
          <option value="Indiana">Indiana</option>
          <option value="Iowa">Iowa</option>
          <option value="Kansas">Kansas</option>
          <option value="Kentucky">Kentucky</option>
          <option value="Louisiana">Louisiana</option>
          <option value="Maine">Maine</option>
          <option value="Maryland">Maryland</option>
          <option value="Massachusetts">Massachusetts</option>
          <option value="Michigan">Michigan</option>
          <option value="Minnesota">Minnesota</option>
          <option value="Mississippi">Mississippi</option>
          <option value="Missouri">Missouri</option>
          <option value="Montana">Montana</option>
          <option value="Nebraska">Nebraska</option>
          <option value="Nevada">Nevada</option>`enter code here`
          <option value="New Hampshire">New Hampshire</option>
          <option value="New Jersey">New Jersey</option>
          <option value="New Mexico">New Mexico</option>
          <option value="New York">New York</option>
          <option value="North Carolina">North Carolina</option>
          <option value="North Dakota">North Dakota</option>
          <option value="Northern Marianas Islands">Northern Marianas Islands</option>
          <option value="Ohio">Ohio</option>
          <option value="Oklahoma">Oklahoma</option>
          <option value="Oregon">Oregon</option>
          <option value="Pennsylvania">Pennsylvania</option>
          <option value="Puerto Rico">Puerto Rico</option>
          <option value="Rhode Island">Rhode Island</option>
          <option value="South Carolina">South Carolina</option>
          <option value="South Dakota">South Dakota</option>
          <option value="Tennessee">Tennessee</option>
          <option value="Texas">Texas</option>
          <option value="Utah">Utah</option>
          <option value="Vermont">Vermont</option>
          <option value="Virginia">Virginia</option>
          <option value="Virgin Islands">Virgin Islands</option>
          <option value="Washington">Washington</option>
          <option value="West Virginia">West Virginia</option>
          <option value="Wisconsin">Wisconsin</option>
          <option value="Wyoming">Wyoming</option>
          <!-- option -->
          </option>
        </select>
      </div>

相关内容

  • 没有找到相关文章