Javascript executed after the load of a PartialView via ajax in a JQuery Dialog works only once -
i know it's long title ^^
i'm using mvc 4 , jquery version 1.8.2.
i'm loading partialview in jquery dialog , works fine.
recently wanted add javascript, hide textbox in dialog, , here starts pain.
the javascript hide code works :
- only once, if close dialog $(this).dialog('close');
 - everytime, if close dialog $(this).remove();
 
i'm not expert in jquery / javascript i'd understand what's going on :)
here's code :
$(function dialoglink() {  // don't allow browser caching of forms $.ajaxsetup({ cache: false });  // wire click event of current or future dialog links $('.dialoglink').on('click', function () {     var element = $(this);      // retrieve values html5 data attributes of link            var dialogtitle = element.attr('data-dialog-title');     var updatetargetid = '#' + element.attr('data-update-target-id');     var updateurl = element.attr('data-update-url');      // generate unique id dialog div     var dialogid = 'uniquename-' + math.floor(math.random() * 1000)     var dialogdiv = "<div id='" + dialogid + "'></div>";      // load form dialog div     $(dialogdiv).load(this.href, function () {                  $(this).dialog({             modal: true,             height: 'auto',             width: 'auto',             resizable: false,             title: dialogtitle,             buttons: {                 "save": function () {                     // manually submit form                                            var form = $('form', this);                     $(form).submit();                 },                 "cancel": function () { $(this).dialog('close'); }                 //"cancel": function () { $(this).remove(); }             }         });          // enable client side validation         $.validator.unobtrusive.parse(this);          // setup ajax submit logic         wireupform(this, updatetargetid, updateurl);           // working once code         $('#possibleanswercontainer').hide();          $('#statisticdatatype').on('change', function (e) {             e.preventdefault();              if ($(this).val() === 'list') {                 $('#possibleanswercontainer').show();             } else {                 $('#possibleanswercontainer').hide();             }         });      });     return false; }); });  function wireupform(dialog, updatetargetid, updateurl) { $('form', dialog).submit(function () {      // not submit if form     // not pass client side validation     if (!$(this).valid())         return false;      // client side validation passed, submit form     // using jquery.ajax form     $.ajax({         url: this.action,         type: this.method,         data: $(this).serialize(),         success: function (result) {             // check whether post successful             if (result.success) {                 // close dialog                 $(dialog).dialog('close');                  // reload updated data in target div                 $(updatetargetid).load(updateurl);              } else {                 // reload dialog show model errors                                    $(dialog).html(result);                  // enable client side validation                 $.validator.unobtrusive.parse(dialog);                  // setup ajax submit logic                 wireupform(dialog, updatetargetid, updateurl);             }         }     });     return false; }); }      
// wire click event of current or future dialog links $('.dialoglink').on('click'   that statement incorrect. on() not work .live() being once attach .live() detect dynamically added elements.
it should be.
$(document).on('click', '.dialoglink'), function(){   to attach future event handlers static elements named, .dialoglink.
also, replace $(document), closest static parent element, such $('#container')
Comments
Post a Comment