jquery查询字符串为空

时间:2018-06-14 06:02:33

标签: jquery sharepoint

我有一个使用LookupField的列表。我在向此列表添加新项目时尝试显示ParentID。这是我的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]);
            }
        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

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

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

这给了我ParentID。