JQuery UI对话框确认传递变量和操作

时间:2013-02-17 11:11:12

标签: jquery jquery-ui

我想将变量传递给 Jquery Ui对话框确认框以执行某些操作 按YES时按下否则关闭对话框 现在我有了这段代码

$("#confirmDialog" ).dialog({
resizable: false,
autoOpen: false,
modal: true,
buttons: {
    'OK': function() {
        $( this ).dialog( "close" );
        },
    'Cancel': function() {
        $( this ).dialog( "close" );
        }
    }
 });


$(".confirm").click(function(){

var myHref = $(this).attr("href");

   $("#confirmDialog").dialog( "open" ).html ( "Confirm?" )
   $("#confirmDialog").on( "dialogclose", function( event, ui ) { window.location = myHref } );
   return false;
});

现在我发现只有函数(“dialogclose”)而没有别的。 当用户按YES时,如何设置ui对话框以执行某些操作? 我试图传递变量但没有成功

$(".confirm").click(function(){    
$("#confirmDialog").attr(myHref).dialog( "open" ).html ( "Confirm?" )
return false;
}); 

$("#confirmDialog" ).dialog({
resizable: false,
autoOpen: false,
modal: true,
buttons: {
    'OK': function() {
        window.location.href = myHref;
        $( this ).dialog( "close" );
        },
    'Cancel': function() {
        $( this ).dialog( "close" );
        }
    }
     });

$("#confirmDialog").dialog( "open" ).html ( "Confirm?" )
$("#confirmDialog").on( "OK", function( event, ui ) { window.location = myHref } );

我该怎么做? 谢谢!

1 个答案:

答案 0 :(得分:1)

您可以编写它以便单击ok按钮(在对话框内),调用一个使用您想要设置的变量的函数。然后在链接单击事件中设置该变量,然后显示对话框以进行确认。

例如:

    var $dialog = $( '#dialog-confirm' );
var $button = $('#linkButton');
var link = '';

// init dialog

$dialog.dialog({
  resizable: false,
  height:340,
  modal: true,
  autoOpen: false,
  buttons: {
    Ok: function() {
      doSomething(); // reference your function here
      $( this ).dialog( "close" );
    },
    Cancel: function() {
      $( this ).dialog( "close" );
    }
  }
});

// bind event

$button.on('click', function(e){
  e.preventDefault();
  link = $(this).attr('href');
  $dialog.dialog('open');
});

function doSomething(){
  window.location = link;
}

这里的工作示例:http://jsbin.com/azezal/2/

此处的代码视图:http://jsbin.com/azezal/2/edit

相关问题