ajax表单提交

ajax表单提交

问题描述:

我正在使用cakephp表单。我有一个下拉选择框。如果下拉值改变,那么表单应该提交。有没有类似于表单提交的方法,比如this.form.submit用于ajax表单。任何帮助?ajax表单提交

如果你使用jQuery你可以使用.serialize()方法和AJAXify一种形式是这样的:

$(function() { 
    $('#myform').submit(function() { 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      data: $(this).serialize(), 
      success: function(result) { 
       // TODO: process the results 
      } 
     }); 
     return false; 
    }); 
}); 

另一种可能性是使用优秀jQuery form plugin

您可以使用下拉elemnts 的onChange事件

Example

$('.target').change(function() { 
    alert('Handler for .change() called.'); 
}); 

如果jQuery是没关系的,你可以做

$('#myDropdown').change(function() { 
    $(this).closest('form').submit(); 
}); 

,如果你想AJAX更换2号线如下

var myForm = $(this).closest('form'); 
$.post(myForm.attr('action'), myForm.serialize(), function(data) 
{ 
    /*do something on success*/ 
}