_spBodyOnLoadFunctionNames.push 保存共享点网站时不起作用

_spBodyOnLoadFunctionNames.push does not work when saving sharepoint site

我已经创建了一种方法来验证共享点站点中的输入字段,您可能会在下面的代码中看到。 问题是该方法应该在保存时激活,但事实并非如此。 现在它在文档准备就绪时被激活,这不是我想要的。 有什么建议吗?

 </script>


 <script type="text/javascript">


  $(document).ready(function(){


 ValidateFields();

});

function ValidateFields()
{
 if ((document.querySelector('input[name$="BooleanField"]').checked ==false) 
 && ($("select[title='Employees selected values'] option").length==0)) 
 {
// $("select[title='Employees selected values'] option").length==0).text("<p> 
  Please check All employees in department OR select employees </p>");
  // // checked, so do something
  alert ("Please check All employees in department OR select employees")
 }

 if ((document.querySelector('input[name$="BooleanField"]').checked ==true) 
 && ($("select[title='Employees selected values'] option").length==1)) {


  alert ("Please check All employees in department OR select employees23")

 }



}

_spBodyOnLoadFunctionNames.push("ValidateFields()");

您需要覆盖表单的 "Submit" 处理程序。根据您按钮的 ID,这可能有效:

$(document).ready(function() {
    $("input[id$='SaveItem']").each(function() {

        // get the button
        var button = $(this);

        // reference the existing handler for later use
        var existingHandler = this.onclick;

        // clear the existing handler
        this.onclick = null;

        // add your custom handler that validates first, then "submits" if successful
        button.click(function() {
            if(ValidateFields()){ // change ValidateFields to return true/false
                 existingHandler(); // validation passed, now call original handler
            }
        });
    });
});

我们可以使用PreSaveAction方法来实现。以下代码供大家参考。

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
function PreSaveAction(){
    if ((document.querySelector('input[name$="BooleanField"]').checked ==false)&& ($("select[title='Employees selected values'] option").length==0)){
        // checked, so do something
        alert ("Please check All employees in department OR select employees");
        return false;
    }
    if ((document.querySelector('input[name$="BooleanField"]').checked ==true)&& ($("select[title='Employees selected values'] option").length==1)) {
        alert ("Please check All employees in department OR select employees23");
        return false;
    }
    return true;
}
</script>