SharePoint Online显示或隐藏列具有相同功能的多个字段

时间:2020-09-16 13:53:19

标签: sharepoint-online

我正在使用以下代码显示或隐藏列来处理SharePoint在线表单。它运行良好,但是无法将三个代码组合在一起,并且默认情况下,在编辑项表单上,所有字段都被隐藏,即使按照代码,它应该根据所选的下拉值显示字段。如果我们再次更改编辑表单中的值,则显示值可以正常工作。

连同此代码,我希望使所有可见字段均为必填字段,我们将不胜感激。

<script src="/sites/XXXXXX/XXXXXXXX/SiteAssets/jquery-1.7.2.min%20-%20Show%20or%20Hide.js" type="text/javascript"> </script>
<script type="text/javascript">

         $(document).ready(function () {
            $('nobr:contains("Person1")').closest('tr').hide();
            $('nobr:contains("Person2")').closest('tr').hide();
            $('nobr:contains("Dropdown")').closest('tr').hide();
            $('nobr:contains("Dropdown1")').closest('tr').hide();
            $('nobr:contains("Text")').closest('tr').hide();
            $('nobr:contains("Dropdown2")').closest('tr').hide();
            $('nobr:contains("Date column")').closest('tr').hide();
            $('nobr:contains("Number filed")').closest('tr').hide();
            $('nobr:contains("Dropdown3")').closest('tr').hide();
            $('nobr:contains("Remarks")').closest('tr').hide();
            $('nobr:contains("Dropdown4")').closest('tr').hide();
            $('nobr:contains("number2)")').closest('tr').hide();
            $('nobr:contains("Dropdown5")').closest('tr').hide();
            $('nobr:contains("Dropdown6")').closest('tr').hide();
            
            $("select[title='Status Required Field']").change(function () {
            console.log("selection changed", $("[title='Status Required Field'] option:selected").text());
            alert($("[title='Status Required Field'] option:selected").text());
                if ($("[title='Status Required Field'] option:selected").text() != "submitted") {
                    $('nobr:contains("Person1")').closest('tr').hide();
                    $('nobr:contains("Person2")').closest('tr').hide();
                    $('nobr:contains("Dropdown")').closest('tr').hide();
                    $('nobr:contains("Dropdown1")').closest('tr').hide();
                    $('nobr:contains("Text")').closest('tr').hide();
                    $('nobr:contains("Dropdown2")').closest('tr').hide();
                    $('nobr:contains("Date column")').closest('tr').hide();
                    $('nobr:contains("Number filed")').closest('tr').hide();
                    $('nobr:contains("Dropdown3")').closest('tr').hide();
                    $('nobr:contains("Remarks")').closest('tr').hide();
                    $('nobr:contains("Dropdown4")').closest('tr').hide();
                    $('nobr:contains("number2")').closest('tr').hide();
                    $('nobr:contains("Dropdown5")').closest('tr').hide();
                    $('nobr:contains("Dropdown6")').closest('tr').hide();

            }
                else {
                    $('nobr:contains("Person1")').closest('tr').show();
                    $('nobr:contains("Person2")').closest('tr').show();
                    $('nobr:contains("Dropdown")').closest('tr').show();
                    $('nobr:contains("Dropdown1")').closest('tr').show();
                    $('nobr:contains("Text")').closest('tr').show();
                    $('nobr:contains("Dropdown2")').closest('tr').show();
                    $('nobr:contains("Date column")').closest('tr').show();
                    $('nobr:contains("Number filed")').closest('tr').show();
                    $('nobr:contains("Dropdown3")').closest('tr').show();
                    $('nobr:contains("Remarks")').closest('tr').show();
                    $('nobr:contains("Dropdown4")').closest('tr').show();
                                                                            
                }
            });
            
        });
</script>

<script src="/sites/XXXXXX/XXXXXXXX/SiteAssets/jquery-1.7.2.min%20-%20Show%20or%20Hide.js" type="text/javascript"> </script>
<script type="text/javascript">

         $(document).ready(function () {
            $('nobr:contains("number2")').closest('tr').hide();
            $('nobr:contains("Dropdown5")').closest('tr').hide();
            $('nobr:contains("Dropdown6")').closest('tr').hide();
            
            $("select[title='Dropdown4']").change(function () {
            console.log("selection changed", $("[title='Dropdown4'] option:selected").text());
            alert($("[title='Dropdown4'] option:selected").text());
                if ($("[title='Dropdown4'] option:selected").text() != "Active") {
                    $('nobr:contains("number2")').closest('tr').hide();
                    $('nobr:contains("Dropdown5")').closest('tr').hide();
                    $('nobr:contains("Dropdown6")').closest('tr').hide();

            }
                else {
                    
                    $('nobr:contains("number2")').closest('tr').show();
                    $('nobr:contains("Dropdown5")').closest('tr').show();                                   
                }
            });
            
        });
</script>

<script src="/sites/XXXXXX/XXXXXXXX/SiteAssets/jquery-1.7.2.min%20-%20Show%20or%20Hide.js" type="text/javascript"> </script>
<script type="text/javascript">

         $(document).ready(function () {
            $('nobr:contains("Dropdown6")').closest('tr').hide();
                        
            $("select[title='Dropdown5").change(function () {
            console.log("selection changed", $("[title='Dropdown5'] option:selected").text());
            alert($("[title='Dropdown5'] option:selected").text());
                if ($("[title='Dropdown5'] option:selected").text() != "Yes") {
                    $('nobr:contains("Dropdown6")').closest('tr').hide();

            }
                else {
                    
                    $('nobr:contains("Dropdown6")').closest('tr').show();                                   
                }
            });
            
        });
</script>```

1 个答案:

答案 0 :(得分:0)

至于隐藏在“编辑”表单上的字段,您的文档就绪功能正在隐藏这些字段。有很多方法可以获取表单状态。您只需检查位置包含“新建”,“显示”或“编辑”表单,然后相应地隐藏字段即可。

        var isNewMode = document.location.pathname.indexOf("/NewForm.aspx") > -1;
        var isDisplayMode = document.location.pathname.indexOf("/DispForm.aspx") > -1;
        var isEditMode = document.location.pathname.indexOf("/EditForm.aspx") > -1;

        if(isNewMode)
        {
            alert("New");
        }

        if(isDisplayMode)
        {
            alert("Display");
        }

        if( isEditMode)
        {
            alert("Edit");
        }

您可以使用PreSaveAction()函数进行自定义验证。单击提交按钮后,将执行此功能。返回true提交表单,否则返回false留在同一页面。

要进行验证,请添加以下代码:

       function PreSaveAction(){
        if($('nobr:contains("someid")').val() == ''){
            alert('Required field'); // some custom validation
            return false;
        }else{
            return true;
        }
    }