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

Jquery 如何提交文件,仅在jqgrid中具有名称的表单元素上循环,jquery,jqgrid,Jquery,Jqgrid,如何使用下面的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)包含没有名称的元素,这导致了此异常。 如何修复此代码? 如何更改此代码,使

如何使用下面的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);
};

您试图设置的值不是一个值。removeAttr只是删除attr…似乎您正试图将(此)元素的“name”数据设置为等于(此),但删除了attr“name”。我不确定这有什么意义

尝试:


不确定你想做什么,但希望这有助于解释你的问题

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

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);
};
但是,如果HTML对象没有名称,它不会引发异常,它将显示为未定义。如图所示:

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

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

你可以看到这是怎么发生的

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