如何从确认对话框中返回布尔值?

时间:2022-03-21 10:24:49

In JavaScript, they have confirm dialog which returns true or false, upon clicking yes or no button.

在JavaScript中,他们有一个确认对话框,在单击yes或no按钮时返回true或false。

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

But it is not customizable and can be stopped upon clicking the check box in the popup.

但它不是可定制的,可以在单击弹出窗口中的复选框时停止。

So I want to use JQuery confirmation or dialogue plugin. However plugin I found, does not returns true or false. It comes with button functions and I can't return true or false from the button functions.

所以我想使用JQuery确认或对话插件。不过我发现插件,没有返回真或假。它带有按钮函数,我不能从按钮函数中返回true或false。

Is there any way to return variable like Boolean in jQuery confirm ?

是否有办法在jQuery确认中返回布尔之类的变量?

3 个解决方案

#1


3  

$('.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 clicked then. For example a dialog has buttons with class, of either true or false. Like

您可以确定当时单击了哪个按钮。例如,一个对话框有带有类的按钮,要么是真,要么是假。就像

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

you can handle the jQuery for this as

您可以为此处理jQuery 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 more functions. But I don't think there are any booleans in JavaScript (jQuery is library of JavaScript; it would only have features JavaScript has).

这只是一种方法,您甚至可以使用数据标记添加更多的函数。但是我认为JavaScript中没有布尔值(jQuery是JavaScript库;它只有JavaScript具备的特性)。

You can try the above Code to test it. It has basic style too, and the buttons are used to determine the user selected true or false thing. It is a custom confirm box. jQuery handles it, and then on a condition it does the job to write the value in the p.

您可以尝试上面的代码来测试它。它也有基本的样式,按钮用来确定用户选择的是真还是假。这是一个自定义确认框。jQuery处理它,然后在条件下将值写入p中。

#2


1  

It can't 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 the returnValue

然后检查返回值

if(returnValue){
}

#3


1  

there is no jquery plugins which allow inline custom confirmation dialogs (return false/true). Each of them work on handling callbacks. And there isn't only jquery ui dialog there are plenty of them. Here is a list of them.

没有jquery插件允许内联自定义对话框(返回false/true)。他们每个人都在处理回调。不仅有jquery ui对话框还有很多。这是他们的名单。

#1


3  

$('.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 clicked then. For example a dialog has buttons with class, of either true or false. Like

您可以确定当时单击了哪个按钮。例如,一个对话框有带有类的按钮,要么是真,要么是假。就像

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

you can handle the jQuery for this as

您可以为此处理jQuery 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 more functions. But I don't think there are any booleans in JavaScript (jQuery is library of JavaScript; it would only have features JavaScript has).

这只是一种方法,您甚至可以使用数据标记添加更多的函数。但是我认为JavaScript中没有布尔值(jQuery是JavaScript库;它只有JavaScript具备的特性)。

You can try the above Code to test it. It has basic style too, and the buttons are used to determine the user selected true or false thing. It is a custom confirm box. jQuery handles it, and then on a condition it does the job to write the value in the p.

您可以尝试上面的代码来测试它。它也有基本的样式,按钮用来确定用户选择的是真还是假。这是一个自定义确认框。jQuery处理它,然后在条件下将值写入p中。

#2


1  

It can't 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 the returnValue

然后检查返回值

if(returnValue){
}

#3


1  

there is no jquery plugins which allow inline custom confirmation dialogs (return false/true). Each of them work on handling callbacks. And there isn't only jquery ui dialog there are plenty of them. Here is a list of them.

没有jquery插件允许内联自定义对话框(返回false/true)。他们每个人都在处理回调。不仅有jquery ui对话框还有很多。这是他们的名单。