如何提交文件,仅循环遍历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);
};
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试设置不是值的值。 removeAttr 只是删除属性...似乎您正在尝试将(此)元素的“名称”数据设置为等于(此),但删除了属性“名称”。我不确定这是否有意义。
尝试:
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:
不太确定您要做什么,但希望这有助于解释您的问题。
但是,如果 HTML 对象没有名称,它不会抛出异常,只会显示为未定义。此处显示: http://jsfiddle.net/ZF3uC/1/
这会引发异常,因为什么你在这里所做的就是说,给我存储在
$(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.
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 is throwing the exception because what you're doing here is saying, give me the
name
property of thename
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 calledremoveAttr
You can see how this is happening here http://jsfiddle.net/ZF3uC/5/