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