Как передать несколько переменных в качестве параметра строки запроса - PullRequest
0 голосов
/ 05 ноября 2019

Я работаю с приведенной ниже функцией Javascript. Это работает только когда значение является числом. Я имею в виду, что он возвращает значение, только если это число. Например:

var ldInstID = getParameterByName("ID")

Если ID - это число, оно работает и присваивает значение переменной, но если ID - строка, это не работает. Пожалуйста, помогите сделать эту работу для строки тоже. Я использую это на странице редактирования списка SharePoint, где ID - это значение столбца списка. Я хочу захватить другой город столбцов и передать его как строку запроса href вместе с идентификатором. На прикрепленных изображениях видно, что ldInstID пустое

<!--
    Name: dispParent.js
-->

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>


<script type="text/javascript">

jQuery(document).ready(function($) {

    //get the ID for the Issue from the Query String
   // var issueID = getParameterByName("ID");
	var ldInstID = getParameterByName("LeadInsitution");
    //find the element with the "Add new item" link.
    //note that if you have more than one list on your page, this just finds the first one
    var anchorElement = $("a[title='Add a new item to this list or library.']");
    
    //modify the "Add new item" link to call the "NewItem2" function and pass in the Issue ID. 
   //Be sure to put the path to your site below. You can use relative URL to the web application or the FQDN
  //  $(anchorElement).attr("href","javascript:NewItem2(event,'URL/Lists/Time/NewForm.aspx?IssueID="  + issueID +  "');");
   // $(anchorElement).attr("href","javascript:NewItem2(event,'URL/NewForm.aspx?IssueID="  + issueID + "&LdInst" + LdInst + "');");
	  $(anchorElement).attr("href","javascript:NewItem2(event,'URL/NewForm.aspx?LdInstID="  + ldInstID +  "');");
    //remove the "onclick" attribute from the anchor element as we aren't using it anymore
    $(anchorElement).removeAttr("onclick");

});


// no, I didn't write this function from scratch, I found it at
// http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
// http://www.sharepointhillbilly.com/Lists/Posts/Post.aspx?ID=26
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>

переданная переменная-ID Переданная переменная-ldInstID

1 Ответ

0 голосов
/ 05 ноября 2019

Получить параметр по идентификатору из URL:

function getURLParameter(parameterName) {
    let result = null, temp = [];
    location.search
        .substr(1)
        .split('&')
        .forEach(function (item) {
            temp = item.split('=');
            if (temp[0] === parameterName)
                result = decodeURIComponent(temp[1]);
        });

    return result;
}

, если мой URL-адрес http://example.com?id1=100&text=my%20text

console.log(getURLParameter('id1')); // 100
console.log(getURLParameter('text')); // "my text"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...