如何提交文件,仅在jqgrid中具有名称的表单元素上循环

时间:2011-09-30 17:23:10

标签: jquery jqgrid

如何使用下面http://jqgrid-php.net的dataProxy在jqGrid中实现文件上传?

运行以下代码会导致异常

Unable to get value of the property 'removeAttr': object is null or undefined 

在第

$(this).data('name', $(this).attr('name')).removeAttr('name');

在代码中的评论中显示。

看起来ele(form)包含没有名称的元素会导致此异常。 如何修复此代码? 如何更改此代码,使其仅在具有名称的元素上循环(保存/恢复名称)。

var dataProxyAjax = function (opts, act) {  // from http://jqgrid-php.net
    opts.url = $(this).getGridParam('url');
    //use normal ajax-call for del
    if (act.substring(0, 4) == 'del_') {
        $.ajax(opts);
    }
    opts.iframe = true;
    var $form = $('#FrmGrid_' + $(this).getGridParam('id'));
    var ele = $form.find('INPUT,TEXTAREA').not(':file');
    //Prevent non-file inputs double serialization
    ele.each(function () {
        // todo: how to fix the error: Unable to get value of the property 'removeAttr': object is null or undefined 
        $(this).data('name', $(this).attr('name')).removeAttr('name');
    });
    //Send only previously generated data + files
    $form.ajaxSubmit(opts);
    //Set names back after form being submitted
    setTimeout(function () {
        ele.each(function () {
            $(this).attr('name', $(this).data('name'));
        });
    }, 200);
};

2 个答案:

答案 0 :(得分:1)

您正在尝试设置不值的内容的值。 removeAttr只是删除了attr ...似乎你试图将(this)元素的'name'数据设置为等于(this)但删除了attr'name'。我不确定这有多大意义。

尝试:

ele.each(function () {
        // todo: how to fix the error: Unable to get value of the property 'removeAttr': object is null or undefined 
        var theName = $(this).attr('name');
        $(this).removeAttr('name');
        $(this).data('name', theName);
    });

答案 1 :(得分:1)

不完全确定您要做的是什么,但希望这有助于解释您的问题。

  

如何更改此代码,使其仅在具有名称的元素上循环(保存/恢复名称)。

ele.each(function () {
    // "only over elements which have name"
    if ($(this).hasAttr('name')) {
        // if it has a name do something with it
    }
});

但是,如果HTML对象没有名称,则不会抛出异常,它只会显示为未定义。如下所示:http://jsfiddle.net/ZF3uC/1/

$(this).data('name', $(this).attr('name')).removeAttr('name');

这是抛出异常,因为你在这里做的是说,给我name中存储的HTML元素的name属性的$(this)属性,然后删除该属性

问题是name属性不存在,因此$(this).data('name', $(this).attr('name'))返回一个未定义的对象,该对象没有名为removeAttr的方法

你可以在http://jsfiddle.net/ZF3uC/5/

看到这是怎么回事