如何获取jQuery Confirm(jConfirm)值并使用该值检查条件

How to Get jQuery Confirm (jConfirm) value and check condition with that value

本文关键字:条件 检查 Confirm 何获取 获取 jQuery jConfirm      更新时间:2023-09-26

我使用了一个jQuery Confirm插件。在我的情况下,jQuery Confirm运行良好。

但我的问题是如何从下面的jconfirm函数中获得返回值。

在这里,我想把jconfirm函数的结果保存到confirmVar中,然后用一些条件来检查它,根据这个条件它会做一些工作。但是这里confirmVar不能得到jconfirm函数的返回值。

  if (courseCredit > remainingCredit) {
        //Here confirmVar cant get the return value.
    
                    var confirmVar = jconfirm({                        
                        theme: 'black',
                        title: 'Course Credit Confirmation',
                        content: 'Are you sure to take this Course ?',
                        confirmButton: 'Yes',
                        cancelButton: 'NO'
                    });
                   //Here I want to check confirmVar
                    if (confirmVar==true) {
                        $.ajax({
                            url: "@Url.Action("AssignCourse", "CourseTeacher")",
                            type: "POST",
                            data: { courseTeacher: [deptCode, tchrMail, creditToTake, remainingCredit, courseCode, crseTchrName, courseCredit] },
                            console: log(data),
                            dataType: "json",
                            success: function (data) {
                            }
                        });
                    }
                    else if(confirmVar == false) {
                      //do nothing
                    }
                    return false;
                }

您可以这样使用它;

var choose=false;
    $.confirm({
       theme: 'black',
       title: 'Course Credit Confirmation',
       content: 'Are you sure to take this Course ?',
       confirmButton: 'Yes',
       cancelButton: 'NO'
        confirm: function(){
            choose=true;
        },
        cancel: function(){
           choose=false;
        }
    });

在javascript的根级别定义变量,并使用confirmcancel状态对其进行更改。希望这对你有用。

更新:

$.confirm({
  theme: 'black',
  title: 'Course Credit Confirmation',
  content: 'Are you sure to take this Course ?',
  confirmButton: 'Yes',
  cancelButton: 'NO'
  confirm: function(){
  $.ajax({
        url: "@Url.Action("AssignCourse", "CourseTeacher")",
        type: "POST",
        data: { courseTeacher: [deptCode, tchrMail, creditToTake, remainingCredit, courseCode, crseTchrName, courseCredit] },
        console: log(data),
        dataType: "json",
        success: function (data) { }
        });
  },
  cancel: function(){
  //do nothing
  }
});