2013-02-06 25 views
0

該代碼將檢測用戶是否對錶單上的某個輸入進行了更改;我的jquery將檢查我是否已將輸入輸入到表單中,但是如何檢測由javascript所做的更改?

$(document).ready(function() { 
    var isDirty = false; 
    $(".formTable :input, select, textarea").change(function() { 
     isDirty = true; 
    }); 

但是在我的表單中我有一個彈出窗口,窗體使用javascript進行更新。上面的代碼沒有選擇這個,所以我能做些什麼呢?

$('.dialogLink').live('click', function() { 
     var element = $(this); 

     // Retrieve values from the HTML5 data attributes of the link   
     var dialogTitle = element.attr('data-dialog-title'); 
     var updateTargetId = '#' + element.attr('data-update-target-id'); 
     var updateTargetId2 = '#' + element.attr('data-update-target-id2'); 

     // Generate a unique id for the dialog div 
     var dialogId = 'uniqueName-' + Math.floor(Math.random() * 1000); 
     var dialogDiv = "<div id='" + dialogId + "'></div>"; 

     // Load the form into the dialog div 
     $(dialogDiv).load(this.href, function() { 
      $(this).dialog({ 
       bgiframe: true, 
       modal: true, 
       width: 500, 
       title: dialogTitle, 
       buttons: { 
        "Add": function() { 
         // Manually submit the form       
         var form = $('form', this); 
         $(form).submit(); 
        }, 
        "Cancel": function() { $(this).dialog('close'); } 
       } 
      }); 
      // Enable client side validation 
      $.validator.unobtrusive.parse(this); 

      // Setup the ajax submit logic 
      wireUpForm(this, updateTargetId, updateTargetId2); 
     }); 
     return false; 
    }); 
}); 

function wireUpForm(dialog, updateTargetId, updateTargetId2) { 
    $('form', dialog).submit(function() { 

     // Do not submit if the form 
     // does not pass client side validation 
     if (!$(this).valid()) 
      return false; 

     // Client side validation passed, submit the form 
     // using the jQuery.ajax form 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      data: $(this).serialize(), 
      success: function (result) { 
       // Check whether the post was successful 
       if (result.success) { 
        // Close the dialog 
        $(dialog).dialog('close'); 

        // UPDATE IS DONE HERE 
        $(updateTargetId).val(result.newTradeName); 
        $(updateTargetId2).val(result.newTradeId); 
        $(updateTargetId).attr("readonly", "readonly"); 
       } else { 
        // Reload the dialog to show model errors      
        $(dialog).html(result); 

        // Enable client side validation 
        $.validator.unobtrusive.parse(dialog); 

        // Setup the ajax submit logic 
        wireUpForm(dialog, updateTargetId, updateTargetId2); 
       } 
      } 
     }); 
     return false; 
    }); 
} 

回答

1

自己觸發事件。

$(updateTargetId).val(result.newTradeName).trigger("change"); 
$(updateTargetId2).val(result.newTradeId).trigger("change"); 
+0

好又簡單。謝謝。 – arame3333