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

问题描述 投票:0回答:1

我正在创建一个屏幕,该屏幕需要根据是否提供了某些字段来将其删除并向选择字段添加一些值。我的表单要求在编辑过程中(而不是在新建过程中)提供某些字段。因此,进入屏幕后,我从状态字段中删除了一些选择选项,尤其是值“建议的”。一旦用户更改了其他字段,我将查看是否现在填充了所有适用的字段,我想重新添加“建议”选项作为选择。在代码末尾附近,这是必需的。该行是:(尽管不起作用)。

            $("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>

先谢谢您。

javascript sharepoint-2013
1个回答
1
投票

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

<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

© www.soinside.com 2019 - 2024. All rights reserved.