【问题标题】:jQuery - Set selected index of dropdown according to its valuejQuery - 根据其值设置下拉列表的选定索引
【发布时间】:2013-04-24 19:37:44
【问题描述】:

我尝试做的是以下。

有人通过 GET 向我的结果页面发送表单。提交结果页面时,URL 如下所示

index.php?Reiseziel=Italy

现在,有一个 ID 为 #filtercountry 的选择,它包含所有可用于过滤的国家/地区。当设置 GET 值 Reiseziel 时,我想遍历 select 的值并选择正确的选项。

例如,选择包含

<select name="Reiseziel" id="filtercountry">
<option value="Please choose..." />
<option value="Germany" />
<option value="Italy" />
<option value="Spain" />

URL 包含“index.php?Reiseziel=Italy”,所以我想将选项值“italy”设置为选中。我将如何实现这一目标?

【问题讨论】:

标签: jquery select


【解决方案1】:

看看这个话题: Selecting option by text content with jQuery

我相信这完全符合您的要求。

【讨论】:

    【解决方案2】:

    使用此功能described here

    function GetURLParameter(sParam)
    {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) 
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) 
            {
                return sParameterName[1];
            }
        }
    }​
    

    然后你可以像这样使用它:

    var reiseziel= GetURLParameter('Reiseziel');
    

    然后你使用变量设置选择的值。

    【讨论】:

      【解决方案3】:

      使用此函数从查询字符串中获取值,它使用RegExp

      function getParameter(name)
      {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.search);
        if(results == null)
          return "";
        else
          return decodeURIComponent(results[1].replace(/\+/g, " "));
      }
      

      然后像这样设置下拉列表的值:

      $('#filtercountry').val(getParameter('Reiseziel'));
      

      或者这个:

      var reiseziel = getParameter('Reiseziel');
      $('#filtercountry').val(reiseziel);
      

      【讨论】:

        【解决方案4】:
         function QueryString(key) {
                        fullQs = window.location.search.substring(1);
                        qsParamsArray = fullQs.split('&');
                        for (i = 0; i < qsParamsArray.length; i++) {
                            strKey = qsParamsArray[i].split("=");
                            if (strKey[0] == key) { return strKey[1]; }
                        }
                    }
        
        var Reisezielnew = QueryString("Reiseziel");
        

        您将在Reisezielnew 中获得country name,您可以随意使用它。

        【讨论】:

          【解决方案5】:
          function getQueryStringFromUrl()
          {
              var vars = [], hash;
              var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
              for(var i = 0; i < hashes.length; i++)
              {
                  hash = hashes[i].split('=');
                  vars.push(hash[0]);
                  vars[hash[0]] = hash[1];
              }
              return vars;
          }
          
           $(document).ready(function () {
                  var name = getQueryStringFromUrl("Reiseziel");
                  $('#dropdownid').val(name);
              });
          

          【讨论】:

            猜你喜欢
            • 2016-07-05
            • 2010-11-21
            • 1970-01-01
            • 2012-06-09
            • 1970-01-01
            • 1970-01-01
            • 2011-09-15
            • 1970-01-01
            相关资源
            最近更新 更多