How to return boolean from confirmation dialog?

In JavaScript, they confirm a dialog that returns true or false after clicking the "Yes" or "No" button.

if (confirm('Your question')) { // do things if OK } 

But it is not configurable and can be stopped when you click on the flag in the pop-up window.

So, I want to use a jQuery confirmation or dialog plugin. However, the plugin I found does not return true or false. It comes with button functions, and I cannot return true or false from button functions.

Is there a way to return a Boolean type variable in jQuery?

+5
source share
3 answers

 $('.dialog button').click(function () { if($(this).attr('class') == 'true') { $('p').text('Ok was clicked'); } else { $('p').text('Cancel was clicked'); } }); 
 .dialog { background-color: #cecece; border: 1px solid #000; padding: 1%; font-family: 'Segoe UI'; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dialog"> <button class="true">Ok</button> <button class="false">Cancel</button> </div> <p></p> 

Answer

You can determine which button was pressed. For example, in the dialog box there are buttons with a class, either true or false. how

 <div class="dialog"> <button class="true">Ok</button> <button class="false">Cancel</button> </div> 

you can handle jQuery for this as

 $('.dialog button').click(function () { if($(this).attr('class') == 'true') { // OK was clicked } else { // cancel was clicked. } } 

This was only one approach, you can even use data- tags to add additional features. But I don’t think that JavaScript has any logical values ​​(jQuery is a JavaScript library, it will only have JavaScript functions).

You can try the above code to check it. It also has a basic style, and buttons are used to determine if the user has chosen the true or false thing. This is a custom confirmation flag. jQuery processes it, and then, provided that the reference value records at p .

+3
source

It cannot return the value, but there is a workaround:

 var returnValue; $("#confirmdialog").dialog({ modal: true, buttons: [{ text: "Yes", "id": "btnOk", click: function () { returnValue = true; }, }, { text: "No", click: function () { returnValue = false; }, }], }); 

And then check returnValue

 if(returnValue){ } 
+1
source

no jquery plugins that allow built-in custom confirmation dialogs (return false / true). Each of them works with callback processing. And there is not only the jquery ui dialog, there are many of them. Here is their list.

+1
source

Source: https://habr.com/ru/post/1203082/


All Articles