JavaScript 设置SharePoint Lookup字段的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 设置SharePoint Lookup字段的值相关的知识,希望对你有一定的参考价值。

<script type="text/javascript">

     

    // This javascript sets the default value of a lookup field identified

    // by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable

    // identified by <<QUERYSTRING VARIABLE NAME>>

     

     

    // Customize this javascript by replacing <<FIELD DISPLAY NAME>> and

    // <<QUERYSTRING VARIABLE NAME>> with appropriate values.

    // Then just paste it into NewForm.aspx inside PlaceHolderMain

     

    _spBodyOnLoadFunctionNames.push("fillDefaultValues");

     

    function fillDefaultValues() {

      var qs = location.search.substring(1, location.search.length);

      var args = qs.split("&");

      var vals = new Object();

      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];

      } 

      setLookupFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);

    }

     

    function setLookupFromFieldName(fieldName, value) {

      if (value == undefined) return;

      var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);

     

    // if theSelect is null, it means that the target list has more than

    // 20 items, and the Lookup is being rendered with an input element

     

      if (theSelect == null) {

        var theInput = getTagFromIdentifierAndTitle("input","",fieldName);

        ShowDropdown(theInput.id); //this function is provided by SharePoint

        var opt=document.getElementById(theInput.opt);

        setSelectedOption(opt, value);

        OptLoseFocus(opt); //this function is provided by SharePoint

      } else {

        setSelectedOption(theSelect, value);

      }

    }

     

    function setSelectedOption(select, value) {

      var opts = select.options;

      var l = opts.length;

      if (select == null) return;

      for (var i=0; i < l; i++) {

        if (opts[i].value == value) {

          select.selectedIndex = i;

          return true;

        }

      }

      return false;

    }

     

    function getTagFromIdentifierAndTitle(tagName, identifier, title) {

      var len = identifier.length;

      var tags = document.getElementsByTagName(tagName);

      for (var i=0; i < tags.length; i++) {

        var tempString = tags[i].id;

        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {

          return tags[i];

        }

      }

      return null;

    }

    </script>

以上是关于JavaScript 设置SharePoint Lookup字段的值的主要内容,如果未能解决你的问题,请参考以下文章

Sharepoint PeopleEditor操作

SharePoint现代页面自定义

JavaScript sharepoint javascript日期格式化程序

SharePoint 2013 使用 JavaScript 获取当前用户

JavaScript sharepoint绝对网站集URL javascript变量

如何从SharePoint列表创建javascript数组?