Passing value in jQuery UI dialog with function

This is my document.ready code:

$(document).ready(function() { $("#dialogbox").dialog({ open: function(event, ui) {$("a.ui-dialog-titlebar-close").remove();}, bgiframe: true,autoOpen: false,closeOnEscape: false,draggable: false, show: "drop",hide: "drop",zIndex: 10000,modal: true, buttons: {'Ok': function() {$(this).dialog("close");processEmp();}} }); }); 

I have the following javascript code that takes one parameter:

 function test(pEmp) { var a = pEmp.value); $('#dialogbox').dialog('open'); } 

My question is based on the value that I pass into my test function, which in turn calls the jQuery UI dialog ('#dialogbox'), when the user clicks the β€œOk” button in the dialog box, I need something (that I not sure how to do this), pass the variable β€œa”, which contains my pEmp.value, to my other function processEmp (a?), which I bound to my button β€œOk”.

Hoping someone can help, since I mostly need this value when the user confirms the dialog box.

Thanks.

+6
source share
2 answers

You can pass a custom option in a dialog before opening it:

 $(function () { $("#dialog").dialog({ open: function (event, ui) { $("a.ui-dialog-titlebar-close").remove(); }, bgiframe: true, autoOpen: false, closeOnEscape: false, draggable: false, show: "drop", hide: "drop", zIndex: 10000, modal: true, buttons: { 'Ok': function () { $(this).dialog("close"); processEmp($(this).data("pEmpValue")); } } }); }); function processEmp(a) { alert(a); } function test(pEmp) { $("#dialog").data("pEmpValue", pEmp.value).dialog("open"); } 

Or even the simplest solution is to declare a variable in the window area:

 var a = null; $(function () { $("#dialog").dialog({ open: function (event, ui) { $("a.ui-dialog-titlebar-close").remove(); }, bgiframe: true, autoOpen: false, closeOnEscape: false, draggable: false, show: "drop", hide: "drop", zIndex: 10000, modal: true, buttons: { 'Ok': function () { $(this).dialog("close"); processEmp(a); } } }); }); function processEmp(a) { alert(a); } function test(pEmp) { a = pEmp.value; $("#dialog").dialog("open"); } 
+6
source

This can be done by adding an event handler to close. Something like that:

 $("#dialogbox").bind("dialogclose", function(event, ui) { processEmp(a); }); 
+4
source

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


All Articles