jquery query string is empty



>我有一个使用LookupField的列表。当我向此列表添加新项目时,我正在尝试显示父 ID。这是我的jquery代码:

<script src="/sites/SWR000744/SiteAssets/Scripts/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
var lookupFieldDisplayName = "PIR2SIR";
//get “ParentID” from Query String
var vals = new Object();
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
for (var i = 0; i < args.length; i++) {
var nameVal = args[i].split("=");
var temp = unescape(nameVal[1]).split('+');
nameVal[1] = temp.join(' ');
vals[nameVal[0]] = nameVal[1];
}
var parentID = vals["ParentID"];
//You should customize the first parameter to match
//the display name of the Lookup field on your child list
setLookup(lookupFieldDisplayName, parentID);
});
function setLookup(fieldTitle, lookupVal) {
//Set default value for lookups with less that 20 items
if ($("select[title='" + fieldTitle + "']").html() !== null) {
$("select[title='" + fieldTitle + "']").val(lookupVal);
} else {
//get the hiddent input using the “optHid” attribute of displayed Input
hiddenInput = $("input[title='" + fieldTitle + "']").attr("optHid");
//set value in the hidden input
$("input[id='" + hiddenInput + "']").attr("value", lookupVal)
//get the string of choices from the input element so we can set displayed value
choices = $("input[title='" + fieldTitle + "']").attr("choices");
//turn choices string into an array so we can iterate through it
choiceArray = choices.split("|");
//improve performance by iterating over every other entry
for (index = 1; index < choiceArray.length; index = index + 2) {
if (choiceArray[index] == lookupVal) {
//set the displayed input which is the PREVIOUS entry in array
$("input[title='" + fieldTitle + "']").val(choiceArray[index - 1]);
}
}
}
}

当我运行代码时,父 ID = 空。另外 qs = 空 我做错了什么?

所以我找到了一个解决方案,我希望它在所有情况下都有效:

var qs = location.search.substring(1, location.search.length);
var parentID = qs.substring(9, qs.indexOf("&"));

这给了我家长ID。

马可

最新更新