可以立即将所有表单绑定到jQuery Form吗?

时间:2015-01-13 21:39:20

标签: jquery forms jquery-forms-plugin

通过ajaxCall我动态创建了一个用户列表,每个用户都采用分离的形式,处理版主动作。像:

<form id="'.$aSpeler['playerID'].'">
  <select name="aktieopspeler">
    <option value="" disabled selected>Kies actie</option>
    <option value="vakantiemodusIN">Vakantiemodus IN</option>
  </select>
  <input type="submit" value="OK">
</form>

我的所有表单都由jQuery Form插件(malsup)处理。 我能想到的唯一解决方案是为每个表单创建绑定,但是是否可以一次绑定所有表单?

我用于现有表单的代码:

var options = { 
    target:'#maincontent',   // target element(s) to be updated with server response 
    url:  'backend/admin_speler.php',    // override for form's 'action' attribute 
    type:  'post'       // 'get' or 'post', override for form's 'method' attribute 
}; 
// bind to the form's submit event 
$('#frm_zoek_speler').submit(function() { 
    // inside event callbacks 'this' is the DOM element so we first 
    // wrap it in a jQuery object and then invoke ajaxSubmit
    $(this).ajaxSubmit(options); 
    return false; 
});

使用:jQuery v2.1.3

1 个答案:

答案 0 :(得分:2)

您可以将单个事件绑定到所有需要的表单的父元素:

$("#parent").on('submit', 'form', function(){
    $(this).ajaxSubmit(options); 
    return false;
});

如果您使用的是1.7之前的jQuery版本,则可以使用:

$("#parent").delegate('form', 'submit', function(){
    $(this).ajaxSubmit(options); 
    return false;
});