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

发布于 2024-12-07 11:02:37 字数 1378 浏览 0 评论 0原文

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

How to implement file upload in jqGrid using dataProxy from http://jqgrid-php.net below?

Running code below causes exception

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

at line

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

as show in comment in code.

It looks like ele (form) contains elements without name which causes this exception.
How to fix this code ?
How to change this code so that it loops (saves/restores name) only over elements which have 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);
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-12-14 11:02:37

您正在尝试设置不是值的值。 removeAttr 只是删除属性...似乎您正在尝试将(此)元素的“名称”数据设置为等于(此),但删除了属性“名称”。我不确定这是否有意义。

尝试:

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);
    });

you are trying to set the value of something that isnt a value. removeAttr just removes the attr...it seems like you are trying to set the data of 'name' for (this) element to be equal to (this) but with the attr 'name' removed. I'm not sure that makes a lot of sense.

try:

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);
    });
桃扇骨 2024-12-14 11:02:37

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

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

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');

这会引发异常,因为什么你在这里所做的就是说,给我存储在 $(this) 中的 HTML 元素的 name 属性的 name 属性,然后删除该属性。

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

您可以在此处查看这是如何发生的 http://jsfiddle.net/ZF3uC/5/

Not exactly sure what you're trying to do but hopefully this helps explain your problem.

How to change this code so that it loops (saves/restores name) only over elements which have name.

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

However, if an HTML object doesn't have a name it won't throw an exception, it will just appear as undefined. Shown here: http://jsfiddle.net/ZF3uC/1/

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

This is throwing the exception because what you're doing here is saying, give me the name property of the name attribute of the HTML element stored in $(this) then remove that attribute.

The problem is the name attribute doesn't exist so $(this).data('name', $(this).attr('name')) is returning an undefined object which has no method called removeAttr

You can see how this is happening here http://jsfiddle.net/ZF3uC/5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文