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
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>
-
It doesn't seem to have any effect on the drop down list of phone numbers.user974061– user9740612018-10-02 17:01:21 +00:00Commented 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– LZ_MSFT2018-10-03 01:02:26 +00:00Commented 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– user9740612018-10-04 14:08:22 +00:00Commented 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– LZ_MSFT2018-10-05 01:08:32 +00:00Commented 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 numberuser974061– user9740612018-10-05 13:17:56 +00:00Commented Oct 5, 2018 at 13:17
