Home  >  Article  >  Web Front-end  >  How to use confirm() method in JavaScript

How to use confirm() method in JavaScript

清浅
清浅Original
2018-12-07 12:35:0612587browse


#Usage of the confirm method: Create a prompt box with confirm and cancel buttons by setting the confirm function to the element. When confirmation is clicked, true is returned and cancel. Returns false

Today I will explain the specific use of the confirm() method in JavaScript. It is mainly used to display a dialog box with a specified message and OK and Cancel buttons. Next, I will explain it in detail in the article. Introduction

[Recommended courses:JavaScript course

How to use confirm() method in JavaScript

confirm(message)

message: refers to the plain text displayed in the pop-up dialog box

Usage:

When the user clicks the OK button, confirm() returns true; if the user clicks the cancel button, confirm() returns false.

It will block all user input to the browser until the OK button or Cancel button is clicked, that is, before the dialog box is closed. So during the process of calling confirm(), JavaScript will pause execution until the user responds

Example:

When we click the button, a dialog box will pop up, and when we click Confirm The page displays confirmation. When you click Cancel, the page displays Cancel

<script type="text/javascript">
   
            function getConfirmation(){
               var result= confirm("这是confirm对话框");
               if( result== true ){
               	document.write("确定")
                  return true;
               }
               else{
               	 	document.write("取消")
                  return false;
               }
            }
 
      </script>

The rendering is as follows:

How to use confirm() method in JavaScript


How to use confirm() method in JavaScript



##Note:

Pay attention to the return problem during use, because confirm has a return value, and the onclick function requires a return value. If the confirm value is not returned, onclick cannot detect the return value, so onclick receives an undefined value.

Summary: The above is all of this article The content is complete. Through this article, I hope everyone can have a certain understanding of the use of the confirm() method in JavaScript


The above is the detailed content of How to use confirm() method in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn