jQuery表单插件,ajaxForm(),但带有条件字段



我需要用ajaxForm()上传一个表单(或者我认为是这样,因为fieldSerialize()似乎无法正确序列化文件字段)。

如何使用ajaxForm()仅序列化表单中字段的子集?我试过在beforeSerialize()或beforeSubmit()中操作arr或$form参数,但没有成功

或者我如何使用ajaxSubmit()或fieldSerialize(),以便它也支持文件上传文件。比方说,对于EG,我只想排除上面有类hiddenField的字段。

谢谢!

难道不简单地将不需要的字段设置为禁用就可以解决这个问题吗?

我认为禁用字段未提交。请参阅jquery源代码>

serializeArray: function() {
    return this.map(function(){
        return this.elements ? jQuery.makeArray( this.elements ) : this;
    })
    .filter(function(){
        return this.name && !this.disabled &&
            ( this.checked || rselectTextarea.test( this.nodeName ) ||
                rinput.test( this.type ) );
    })

编写一个自定义序列化函数,如下所示:注意jQuery将过滤掉任何具有"hiddenField"类属性值的输入

  function scrapeFormData(formName){
var frm_data = "";
$('#'+formName+' :input').not('.hiddenField').each(function(){
    switch(this.type){
        // handle checkbox specially 
        // I persisted my checkbox field this way. You can do this in a better way
        case 'checkbox': {
                if ($(this).attr('checked') == 'checked'){
                    frm_data += $(this).attr('id') + "=1&";// checked value 1
                }else{
                    frm_data += $(this).attr('id') + "=0&";// unchecked value 0
                }
                break;
        }
        // handle radio button specially
        // I persisted my radio button field this way. You can do this in a better way
        case 'radio': {
                if ($(this).attr('checked') == 'checked'){
                    //alert('checked');
                    frm_data += $(this).attr('id') + "=" + $(this).attr('value')+"&";
                }                   
                break;
        }
       default:{
                frm_data += $(this).attr('id') + "=" + $(this).attr('value')+"&";
                break;
        }
    }
});   
frm_data = frm_data.substring(0,(frm_data.length - 1));
return frm_data;

}

最新更新