在新建或编辑表单中添加和删除选择字段的选项

Adding and removing options for a choice field in New or Edit form

我正在创建一个屏幕,它需要根据是否提供了某些字段来删除和添加一些值到选择字段。我的表单要求在编辑期间(而不是在新建期间)提供某些字段。因此,在进入屏幕后,我从“状态”字段中删除了一些选项,特别是值 "Proposed"。一旦用户更改了其他字段,我会查看现在是否填充了所有适用字段,我想重新添加 "Proposed" 选项作为选择。这是下面需要的,靠近代码末尾。该行是:(虽然不起作用)。

            $("select[title='Status'] option").add(ProposedOption);

这是我的代码:

<script type="text/javascript">
$(document).ready(function() {
   //don't exectute any jsom until sp.js file has loaded.          
   SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ChkUser);
});
function ChkUser()
{
        //var admingroup = "DMSDataManagement Owners";
        //console.log('selected='+'Admingroup='+admingroup);    
        //Lozzi.Fields.disableWithAllowance("Status",[admingroup]);
        //Lozzi.Fields.disable("Status");
        Lozzi.Fields.hide("ApprovedBy");
        Lozzi.Fields.hide("DateApproved");
        Lozzi.Fields.hide("PreviousStatus");

        var selectedValue = ($("h3:contains('Status')").closest('tr').find('select').val());
        //alert('Selected='+selectedValue);
        ProcessStatusValues(selectedValue);

        //on change of dropdown of Is this Critical we will call this
        $("h3:contains('DataDomain')").closest('tr').find('select').change(function () {
            //CheckMandatory(selectedValue);
            ProcessStatusValues(selectedValue);
        });
}
function ProcessStatusValues(selectedValue) {
        var ProposedFound = false;
        var ProposedOption;

        $("select[title='Status'] option").each(function(){
            if (selectedValue == 'In Process'){
               //console.log('value='+$(this).text());
           if(($(this).text() == 'Approved') || ($(this).text() == 'Obsolete'))
               {
               $(this).remove();
           }
               else if ($(this).text() == 'Proposed')
               {
                    console.log('found Proposed');
                   //ProposedFound = true;
                   if ($("h3:contains('DataDomain')").closest('tr').find('select').val() == '0')
                   {
                       ProposedOption = $(this);
                   $(this).remove();
                   } 
               } 
            }
            if (selectedValue == 'Proposed'){
            if(($(this).text() == 'Approved') || ($(this).text() == 'Obsolete'))
                {
               $(this).remove();
                       ProposedFound = true;
        }
            }
    })
       if (($("h3:contains('DataDomain')").closest('tr').find('select').val() != '0') && (ProposedFound == false)) {
            console.log('need to add Proposed');
            $("select[title='Status'] option").add(ProposedOption);
       }
}
</script>

提前致谢。

将建议的选项添加到 select 下拉列表中,如下所示:

<script type='text/javascript'>
$( document ).ready(function() {
 $('select[title="Status"]').append(`<option value="Proposed">Proposed</option>`); 

});

</script>

参考:

Add and Remove Options in Select using jQuery