使用下拉 Rest API Sharepoint Online 的相应数据填充文本框

Fill textbox with corresponding data of dropdown RestAPI Sharepoint Online

下面的工作代码加载了从 RestAPI 调用返回的下拉列表 ID REG 的选项值。

我想获取 Title 的值并将其相应的 family 值(在同一个 RestAPI 调用中返回)填充到带有更改 javascript 事件的文本框 id Family 中。请指教

<script>
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAR);
var jresult;
function getAR() {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('List_Name')/items?$Select=Title,Family",
        type: "GET",
        headers: {"accept": "application / json;odata = verbose",},
        success: function(data) {
            jresult = data.d.jresult;
            var options = "";
            for(var i = 0; i < jresult.length; i++){
                options = options + "<option value='" + jresult[i].Title + "'>" + jresult[i].Title + "</option>";
            }
            $("#REG").append(options);
        },
        error: function(error) {alert(JSON.stringify(error));}
    });
}
function fillFamily() {
  var x = document.getElementById("REG").value;
  $( "Family" ).val( x.Family );
}
</script>

<select id="REG" name="REG" class="custom-select" required="required" onchange="fillFamily()"></select>
<input type="text" id="Family" name="Family">

我已经更新了你的代码,你可以试试。更正了您的代码中的错误。

 <script>
        $(function(){
            getAR() 
        })
        var jresult;
        function getAR() {
            $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('2/23')/items?$Select=Title",
                type: "GET",
                headers: {"accept": "application / json;odata = verbose",},
                success: function(data) {
                    jresult = data.d.results;
                    var options = "";
                    for(var i = 0; i < jresult.length; i++){
                        options = options + "<option value='" + jresult[i].Title + "'>" + jresult[i].Title + "</option>";
                    }
                    $("#REG").append(options);
                },
                error: function(error) {alert(JSON.stringify(error));}
            });
        }
        function fillFamily() {
           
          var x = document.getElementById("REG").value;
          $( "#Family" ).val( x );
        }
        </script>
        
        <select id="REG" name="REG" class="custom-select" required="required" onchange="fillFamily()"></select>
        <input type="text" id="Family" name="Family">

更新:

<script>
    $(function(){
        getAR() 
    })
    var jresult;
    function getAR() {
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('2/23')/items?$Select=Title,Family",
            type: "GET",
            headers: {"accept": "application / json;odata = verbose",},
            success: function(data) {
                jresult = data.d.results;
                var options = "";
                for(var i = 0; i < jresult.length; i++){
                    options = options + "<option value='" + jresult[i].Title + "'Family="+jresult[i].Family+">" + jresult[i].Title + "</option>";
                }
                $("#REG").append(options);
            },
            error: function(error) {alert(JSON.stringify(error));}
        });
    }
    function fillFamily() {
        var val=$("#REG").find("option:selected").attr("Family");
      $( "#Family" ).val( val );
    }
    </script>
    
    <select id="REG" name="REG" class="custom-select" required="required" onchange="fillFamily()"></select>
    <input type="text" id="Family" name="Family">