0

I have a list of four phones in a dropdown list. The user can choose one of the phones and enter a checked out date. I want to modify the drop down list so if the phone and a checked out date is already on the list, the user won't see that phone on the drop down list. How can I do this?

1 Answer 1

0

The following example for your reference.

1.Create a custom list, and add choice field "Phones" and a date field "CheckedOutDate". 2.Add the following code into script editor web part in new form page.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    $("select[title^='Phones'] option").each(function(){
        if($(this).val()!=""){
            if(checkPhoneExists($(this).val())){
                $(this).remove();
            }
        }
    });
})
function checkPhoneExists(phone){
    var flag=false;
    var listID=_spPageContextInfo.pageListId.replace("{","").replace("}","");
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'"+listID+"')/items?$filter=Phones eq '"+phone+"'",
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            if(data.d.results.length>0){
                flag=true;
            }

        },
        error: function (data) {
            //alert("Error");
        }
    });
    return flag;
}
</script>

enter image description here

6
  • It doesn't seem to have any effect on the drop down list of phone numbers.
    – user974061
    Oct 2, 2018 at 17:01
  • In my test the field name is "Phones", did you use the same name? if not, modify it in the code above.
    – LZ_MSFT
    Oct 3, 2018 at 1:02
  • Names are the same. I'm using SP 2013 online. I created the list, went to the form and added the web part and pasted the above code into the script editor. I added one new item to the list using the first phone number from the drop down and gave it today's date as CheckedOutDate. Went back to add another item and complete list of phone numbers was still available.
    – user974061
    Oct 4, 2018 at 14:08
  • Check if the "Phones" and "CheckedOutDate" are internal name and make sure the "Phones" field is not a lookup field.
    – LZ_MSFT
    Oct 5, 2018 at 1:08
  • Phones is type: choice with three phone numbers, require column contains information = no and enforce unique values = yes, drop-down menu, allow fill in = no, default choice is the 1st phone number
    – user974061
    Oct 5, 2018 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.