JavaScript确认:选择是或否

Wordpress8个月前发布 SUYEONE
1.9K 0 0

In this article, we’ll explore how to utilize JavaScript to present a confirmation dialog to users. Confirmation dialogs enable you to perform Actions based on user input. As one of the core Web technologies, JavaScript is widely used on most websites and is supported by all modern browsers without requiring plugins. At Envato Tuts+, we delve into tips and techniques to AId in your daily JavaScript development.

As a JavaScript developer, you often need to gather user input in the form of yes or no questions and execute actions accordingly. Certain operations can be sensitive and irreversible, so it’s essential to warn users or confirm their intent to prevent accidental actions. For instance, when there’s a delete link that rEMOves entities from a Database, you’d want to confirm with the user before proceeding, giving them a chance to cancel if they mistakenly clicked.

Here, we’ll discuss two approaches to seeking user confirmation in JavaScript: using the `confirm` method and a hidden confirm div.

### Using the `confirm` Method

The `confirm` method, part of the `window` object in JavaScript, allows you to display a dialog box and wait for the user’s response. Let’s examine its syntax and usage.

#### The `confirm` Method Syntax

The syntax for the `confirm` method is as follows:

“`javascript
var result = window.confirm(message);
“`

The `confirm` method takes a single string parameter, which is the message you want to display in the dialog box. This parameter is optional but should convey a meaningful message to avoid a blank dialog box with only “OK” and “Cancel” options. Typically, the message is posed as a question, offering two choices for the user.

The dialog box contains two buttons: OK and Cancel. If the user clicks OK, the `confirm` method returns `true`, while clicking Cancel results in `false`. You can use the return value of the `confirm` method to determine the user’s choice. If you prefer “Yes” and “No” instead, I’ll show you how further down.

Since the `window` object is always implicit, meaning its properties and methods are always in scope, you can also call the `confirm` method as shown below:

“`javascript
var result = confirm(message);
“`

It’s worth noting that the confirmation dialog is Modal and synchronous. JavaScript execution pauses until the user closes the dialog by clicking either OK or Cancel.

### A Real-World Example with `confirm`

Let’s illustrate the usage of the `confirm` method with a practical example.

When the “Delete My Profile!” button is clicked, it calls the `deleteProfile` function. Inside this function, we invoke `confirm`, presenting a confirmation dialog to the user. If the user clicks OK, they will be redirected to `/deleteProfile.php`, where the deletion process would take place. Conversely, if they click Cancel, no action is taken, and JavaScript execution resumes after the dialog is closed.

### Using a Hidden Div for Confirmation

The `confirm` method has some drawbacks. Primarily, the dialog box doesn’t integrate with your application or website’s UI, nor does it reflect your branding or color scheme. It’s not customizable, like changing “OK” and “Cancel” to “Yes” and “No.” AdDiTionally, it’s modal, preventing interaction with other parts of the interface while displayed.

An alternative approach is to use a hidden div on the page for confirmation. Here’s an example:

In this case, we have a hidden confirmation div with the ID “confirm.” To show the div, we simply set its `hidden` attribute to `false`. When we want to display the confirmation message, we set `hidden` to `true`, then back to `false` to hide it again. This method offers more flexibility and customization for the confirmation process.

### Conclusion

Today, we’ve examined two ways to obtain user confirmation in JavaScript. We started with the straightforward `confirm` method, but acknowledged its limitations in providing a seaMLess user experience. Then, we demonstrated how to use a hidden div for more control over the appearance and behavior of the confirmation process. By choosing the appropriate method, you can create a more intuitive and engaging user interface.

© 版权声明

相关文章

暂无评论

暂无评论...
☺一键登录开启个人书签等功能!