内容编辑器Web部件(CEWP)中的JavaScript逻辑

时间:2019-02-25 00:19:03

标签: javascript sharepoint cewp

我尝试根据SharePoint列表中的条件(其他字段中的值)隐藏编辑表单中的字段。下面的代码可以隐藏字段,但是逻辑现在不起作用。使用alert,我可以看到在字段中选择的不同值,但是条件语句没有像我期望的那样将fldList重置为空。任何帮助,将不胜感激。我是JS的新手。

<script src="https://code.jquery.com/jquery-latest.min.js"></script><script>

function HideFields() {
    //Enter the fields you would like to hide here.
    fieldsToHide = fldList;

    //Get all SharePoint fields
    var formFieldTitles = $(".ms-formtable td.ms-formlabel h3.ms-standardheader");

//Iterate over each row in the form
formFieldTitles.each(function () {

    //Get the text of the field title
    var textToMatch = $(this).text();

    //Get the table row associated with this title
    var currentRow = $(this).closest('tr');

    //Iterate over our list of fields we wish to hide
    for (var i = 0; i < fieldsToHide.length; i++){
        var field = fieldsToHide[i];

        //Match the SharePoint field name to our field name
        if (textToMatch.toLowerCase().replace("*", "").trim() === field.toLowerCase().replace("*", "").trim()){

            //Hide this field
            $(currentRow).hide();    
            }
        }
    });
}

function AddToBodyOnLoad(){
    //Ensure that our function is called last by pushing it again
    _spBodyOnLoadFunctionNames.push("HideFields");
}

$(document).ready(function () {
    var value = $("select[title='Activity Type Required Field'] option:selected").text();

    if (value = 'New'){
        fldList = ["Additional Information Required from Applicant", "Assigned To (Field)", "Date Lands Officer received", "Date Lands Officer started merit review", "External Referral Required", "External Reviewer", "Inspection", "Internal Referral Required", "Internal Reviewer", "Merit Recommendation by Field", "Merit Upload to ECM complete", "Referral Due Date", "Zone", "FNC", "Merit Decision Letter", "Review Merit Recommendation by PAS", "Security"];
    }
    else if (value = 'Renewal'){
        fldList = [];
    }
    else{
        fldList = [];
    }

});

//Add our function to the array of onload functions
_spBodyOnLoadFunctionNames.push("AddToBodyOnLoad");</script>

1 个答案:

答案 0 :(得分:0)

如果您要按归档标题隐藏field(tr),则可以尝试使用$('nobr:contains("field title")'),对于JavaScript / jQuery脚本,始终使用开发人员工具(F12)进行调试会很有帮助。 / p>

$('nobr:contains("field title")').closest('tr').hide();

这与我在SharePoint 2013中的示例代码类似。

http://php.net/manual/en/function.array-merge.php