如何在JavaScript下拉菜单中添加“请选择”

时间:2019-07-30 20:03:47

标签: javascript sharepoint-2013

JavaScript会在SharePoint列中填充下拉选项,我需要添加“请选择”或空白(不会为NOT Null传递)。

我尝试将新项目添加到SharePoint列表中并附加到JavaScript,但是都没有给出我想要的结果。

function getReasons(ddlReason)
{
    $.ajax({ 
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Reason for Rejection')/Items?$orderby=Title", 
       type: "GET", 
       async: false,
       headers: {"accept": "application/json;odata=verbose"}, 
       success: function (data) { 
          if (data != undefined && data.d != undefined && data.d.results != undefined && data.d.results.length > 0) { 
          /////////////
            $.each(data.d.results, function( index, value ) 
            {
                var option = document.createElement("option");
                option.text = value.Title;
                option.value = value.Id;
                ddlReason.appendChild(option);
            });
        } 
       }, 
       error: function (xhr) { 
          alert(xhr.status + ': ' + xhr.statusText); 
       } 
    }); 
}=

我正在从SharePoint获取选项... “图像不良” “编码”

我想在选项顶部添加... “请选择”或空白

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以仅在.each循环之前调用.appendChild

        var option = document.createElement("option");
        option.text = "Please Select";
        option.value = "-1";
        ddlReason.appendChild(option);

        $.each(data.d.results, function( index, value ) 
        {
            var option = document.createElement("option");
            option.text = value.Title;
            option.value = value.Id;
            ddlReason.appendChild(option);
        });