Javascript pop-up confirmation box when a field contains a certain value but clicking 'Ok' doesn't submit the form?

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/247061

  •  21-01-2021
  •  | 
  •  

Question

I have a Script Editor web part running on a 'NewForm.aspx' page that contains some javascript that pops up a confirmation box if a certain field contains a certain line of text when the user is creating a new item on the list.

If they click 'Ok' I want the form to submit (create the new item in the list), if they click 'Cancel' then return to the form submission page without submitting the form.

So far the pop-up shows up when they enter the 'test' into the 'Title field', and the 'Cancel' button works (by just returning them to their form without submitting), but clicking 'Ok' doesn't submit the form. It also just returns them to the form submission page as if they pressed 'Cancel.'

Here's my code:

<script type="text/javascript" src="/sites/gameops/SiteAssets/jquery-1.12.1.js"></script>
<script type="text/javascript">
 var j = jQuery.noConflict(); 
 function PreSaveAction() { 
 var txtTitle = j(":input[title='Title']").val(); 
   if(txtTitle == 'test'){        
   confirm("Please enter a title");   //write popup code here       
   return false;
}
else{
   return true;
    }
}
</script>
Était-ce utile?

La solution

I am assuming you are simply trying to use SharePoint client side form validation using the PreSaveAction method.

Your issue is your code is always returning false which is the same thing as making validation fail everytime. What you need to do is retrieve the boolean result from the Confirm() method and return the proper boolean value. If the user clicks "Okay", then return True, otherwise false.

Hopefully this code will work for you and give you an idea on what you need to do:

<script type="text/javascript" src="/sites/gameops/SiteAssets/jquery-1.12.1.js"></script>
<script type="text/javascript">
 var j = jQuery.noConflict(); 
 function PreSaveAction() { 
   var txtTitle = j(":input[title='Title']").val(); 
   if(txtTitle == 'test'){        
      var result = confirm("Please enter a title"); //write popup code here
      if(result == true) {
        return true;
      }
      else{       
        return false;
      }
   }
   else{
     return true;
   }
}
</script>

Autres conseils

I thought I'd rewrite the other guys answer as I feel it is much better for beginners to learn in vanilla JS or else you end up with bad habits. I have also changed the code so that it should automatically find your form and submit it.

<script type="text/javascript">
 function PreSaveAction() { 
 var txtTitle = document.querySelector("input[title='Title']").value; 
   if(txtTitle == 'test'){        
      var result = confirm("Please enter a title");   //write popup code here
      if(result == true) {
        document.querySelector('form[action="' + window.location.pathname + '"]').submit();
      }       
      return false;
   }
   else{
     return true;
   }
}
</script>
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top