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

时间:2016-05-05 15:59:02

标签: javascript jquery asp.net-mvc jquery-plugins

我使用了jQuery Confirm plugin。在我的情况下,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;
                }




1 个答案:

答案 0 :(得分:0)

你可以使用类似的东西;

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
  }
});