SHAREPOINT - 限制重复输入

SHAREPOINT -Restrict duplicate Entry

伙计们需要支持以下场景 我有一份包含日期和时间的预订清单。我想按日期和时间限制重复条目 我已经尝试了其余 Api 代码,但它不起作用.. 请支持在我的代码下方找到

我试图检查一个条件..我需要检查两个条件

请多多支持

<script src="/Departments/WDCI/UIJP/PublishingImages/jquery-1.12.4.js" type="text/javascript"></script><script type="text/javascript">
        function PreSaveAction() {
            var check = false;
            var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
            var DateId = $('input[id*="Date_Req"]');
            
            $.ajax({
                //replace the rest api to filter items based on your files
   url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + listid + "')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter=Date_Req eq  + DateId + ",
                                type: 'GET',
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                },
                success: function (data, textStatus, jqXHR) {
                    var count = data.d.results.length;
                    if (count < 2) {
                        check = true;
                    } else {
                        alert('Dear Employee,This Calander Year you have already applied for two Job Positions.So you are eligible to apply next year only');
                     ddwrt:GenFireServerEvent('__commit;__redirect={/SiteAssets/Thank%20You.aspx}')
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            })
        
                           return check;
        }
</script>

<script src="jquery-1.12.4.js" type="text/javascript">
</script><script type="text/javascript">
        function PreSaveAction() {
            var check = false;
            var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
            var DateId = $('input[id*="Date_Req"]');
            
            $.ajax({
                //replace the rest api to filter items based on your files
   url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + listid + "')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter=Date_Req eq  + DateId + ",
                                type: 'GET',
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                },
                success: function (data, textStatus, jqXHR) {
                    var count = data.d.results.length;
                    if (count < 1) {
                        check = true;
                    } else {
                        alert('Dear Employee,there is no slot available during this period');
                     ddwrt:GenFireServerEvent('__commit;__redirect={/SiteAssets/Thank%20You.aspx}')
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            })
        
                           return check;
        }
</script>

您可以使用以下代码来限制重复输入:

<script type="text/javascript">
        function PreSaveAction() {
            var check = false;
            //var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
            var DateId = $("input[title='Date_Req']").val();
            alert(DateId);
            $.ajax({
                //replace the rest api to filter items based on your files
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists//GetByTitle('List0325')/items?$select=ID,Date_Req,Title&$orderby=ID desc&$filter=Date_Req eq '"  + DateId + "'",
                type: 'GET',
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                },
                success: function (data, textStatus, jqXHR) {
                    var count = data.d.results.length;
                    if (count < 1) {
                        check = true;
                    } else {
                        alert('Dear Employee,there is no slot available during this period');
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            })
        
                           return check;
        }
</script>

注意:您需要更改以下内容:

  1. var DateId = $("input[title='Date_Req']").val() 中,将“Date_Req”更改为您的日期列名称;
  2. 在 $.ajax() 中,将 url 部分
  3. 中的列表标题和列名称更改为您的名称

结果是这样的:

<script type="text/javascript">
       function PreSaveAction() {
           var check = false;
           //var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
           var DateId = $("input[Title*='Req_Date']").val();
           var TimeID= $("select[Title*='Req_Time'] option:selected").text();


           alert(DateId);
           alert(TimeID);
           $.ajax({
 //replace the rest api to filter items based on your files 
 url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists//GetByTitle('Discussion2')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter= Req_Date eq '" + DateId + "' and Req_Time eq '" + TimeID + "'",                type: 'GET',
               async: false,
               headers: {
                   "accept": "application/json;odata=verbose",
                   "content-type": "application/json;odata=verbose",
               },
               success: function (data, textStatus, jqXHR) {
                   var count = data.d.results.length;
                   if (count < 1) {
                       check = true;
                   } else {
                       alert('Dear Employee,there is no slot available during this period');
                   }
               },
               error: function (jqXHR, textStatus, errorThrown) {
                   alert(textStatus);
               }
           })
       
                          return check;
       }
</script>```