根据url参数设置下拉值

时间:2017-06-13 19:27:04

标签: javascript html asp.net

我需要根据url参数设置下拉列表项。

我有这个javascript脚本,它从url中提取参数:

    function getAllUrlParams(url) {

  // get query string from url (optional) or window
  var queryString = url ? url.split('?')[1] : window.location.search.slice(1);

  // we'll store the parameters here
  var obj = {};

  // if query string exists
  if (queryString) {

    // stuff after # is not part of query string, so get rid of it
    queryString = queryString.split('#')[0];

    // split our query string into its component parts
    var arr = queryString.split('&');

    for (var i=0; i<arr.length; i++) {
      // separate the keys and the values
      var a = arr[i].split('=');

      // in case params look like: list[]=thing1&list[]=thing2
      var paramNum = undefined;
      var paramName = a[0].replace(/\[\d*\]/, function(v) {
        paramNum = v.slice(1,-1);
        return '';
      });

      // set parameter value (use 'true' if empty)
      var paramValue = typeof(a[1])==='undefined' ? true : a[1];

      // (optional) keep case consistent
      paramName = paramName.toLowerCase();
      paramValue = paramValue.toLowerCase();

      // if parameter name already exists
      if (obj[paramName]) {
        // convert value to array (if still string)
        if (typeof obj[paramName] === 'string') {
          obj[paramName] = [obj[paramName]];
        }
        // if no array index number specified...
        if (typeof paramNum === 'undefined') {
          // put the value on the end of the array
          obj[paramName].push(paramValue);
        }
        // if array index number specified...
        else {
          // put the value at that index number
          obj[paramName][paramNum] = paramValue;
        }
      }
      // if param name doesn't exist yet, set it
      else {
        obj[paramName] = paramValue;
      }
    }
  }

  return obj;
}

正确获取参数,但我无法设置IF以更改下拉值。

这是HTML

<div class="form-group">

                    <div class="col-xs-6 col-sm-3">
                        <asp:DropDownList runat="server" ID="ddlSite2" CssClass="form-control" AutoPostBack="false" >
                            <asp:ListItem Text="Job Start" Value="3" />
                            <asp:ListItem Text="In Progress" Value="1" />
                            <asp:ListItem Text="Final Inspection" Value="2" />
                        </asp:DropDownList>
                    </div>
                </div>

我的javascript代码不起作用:

     var endpage = getAllUrlParams().endpage;
         if ( endpage.value = "logininprogress" ) {
            document.getElementById('ddlSite2').value = '1';
                }

        if ( endpage.value = "loginfinal" ) {
            document.getElementById('ddlSite2').value = '2';
                }

        if (endpage.value = "loginjobstart" ) {
            document.getElementById('ddlSite2').value = '3';
                }

网址:https://localhost/blabla/bla/endpage=logininprogress

1 个答案:

答案 0 :(得分:1)

您正在设置endpage.value而不是检查它。在JavaScript中=设置一个值,===用于检查相等性。

endpage.value = "logininprogress"更改为endpage.value === "logininprogress"并对其他if语句进行相同的更改。