jQuery的奇怪行为';s clone()和一个POST请求
在使用jQuery时,我试图通过单击按钮动态地增加表单元素(文件上传框)。代码如下:jQuery的奇怪行为';s clone()和一个POST请求,jquery,forms,post,Jquery,Forms,Post,在使用jQuery时,我试图通过单击按钮动态地增加表单元素(文件上传框)。代码如下: $(document).ready(function () { $('#new-image').click(function () { var idx = $('.input.file').size(); var upload = $('.input.file:first-child').clone(); $(upload.find('label')).a
$(document).ready(function () {
$('#new-image').click(function () {
var idx = $('.input.file').size();
var upload = $('.input.file:first-child').clone();
$(upload.find('label')).attr('for', 'Attachment'+idx+'File');
$(upload.find('input[type=file]')).attr('name', 'data[Attachment]['+idx+'][file]');
$(upload.find('input[type=file]')).attr('id', 'Attachment'+idx+'File');
upload.insertBefore('#new-image');
return false;
});
});
问题是,如果我尝试修改输入的名称,我最终会在post请求中得到类似的结果——摘自Chrome的(dev build)控制台,也在Firefox(3.6)上出现
这不是因为字符串连接,我尝试使用硬编码的值,但请求中的奇怪之处仍然存在。我是在这里遗漏了什么,还是这是jQuery中的一个bug
(如果有人想知道,名称属性格式-data[…
来自CakePHP)
使现代化 问题似乎不是因为
.attr()
,而是因为.clone()
。我相应地修改了这个问题
我的印象是这样做有效:
upload.find('input[type=file]').name = 'data[Attachment]['+idx+'][file]';
// wrong -> find returns a jQuery object and setting name has no effect
因为我没有尝试添加多个文件,所以我只是尝试了最后添加的字段:)。它甚至不能以正确的形式工作:
upload.find('input[type=file]').get(0).name = 'data[Attachment]['+idx+'][file]';
// I still get mumbo-jumbo in the post, between the ][ characters
我刚尝试过不使用克隆(),它可以工作,这次是真的:)
$(“#新建图像”)。单击(函数(){
var idx=$('.input.file').size();
var upload=$('File');
upload.insertBefore(“#新图像”);
返回false;
});
有人知道为什么克隆()会这样吗?试试这个:
<div class="file">
<div>
<label for="Attachment0File">File: </label>
<input type="file" name="data[Attachment][file][]" id="Attachment0File" />
</div>
</div>
<button type="button" id="new-image">New</button>
$(document).ready(function () {
$('#new-image').click(function (e) {
e.preventDefault();
var idx = $('.file').length;
$('.file:first-child').clone(true, true)
.find('label').attr('for', 'Attachment'+idx+'File').end()
.find('input[type="file"]').attr('name', 'data[Attachment][file][]').attr('id', 'Attachment'+idx+'File').end()
.insertBefore('#new-image');
});
});
文件:
刚出现的
$(文档).ready(函数(){
$(“#新建图像”)。单击(函数(e){
e、 预防默认值();
var idx=$('.file').length;
$('.file:first child').clone(true,true)
.find('label').attr('for','Attachment'+idx+'File').end()
.find('input[type=“file”]').attr('name','data[Attachment][file][]).attr('id','Attachment'+idx+'file')。end()
.insertBefore(“#新图像”);
});
});
上载不需要用$()包装,因为它已经是jQuery对象
<div class="file">
<div>
<label for="Attachment0File">File: </label>
<input type="file" name="data[Attachment][file][]" id="Attachment0File" />
</div>
</div>
<button type="button" id="new-image">New</button>
$(document).ready(function () {
$('#new-image').click(function (e) {
e.preventDefault();
var idx = $('.file').length;
$('.file:first-child').clone(true, true)
.find('label').attr('for', 'Attachment'+idx+'File').end()
.find('input[type="file"]').attr('name', 'data[Attachment][file][]').attr('id', 'Attachment'+idx+'File').end()
.insertBefore('#new-image');
});
});
文件:
刚出现的
$(文档).ready(函数(){
$(“#新建图像”)。单击(函数(e){
e、 预防默认值();
var idx=$('.file').length;
$('.file:first child').clone(true,true)
.find('label').attr('for','Attachment'+idx+'File').end()
.find('input[type=“file”]').attr('name','data[Attachment][file][]).attr('id','Attachment'+idx+'file')。end()
.insertBefore(“#新图像”);
});
});
上传不需要用$()包装,因为它已经是一个jQuery对象了。它不应该是:
var idx=$('.input.file').length;
?size()
同样有效:。我应该切换到长度
,但这更多的是一个实验,而不是影响生产的东西,所以我没有太多注意:)。尝试复制但失败:你能更新那里的代码并复制你描述的吗?给你:。克隆元素时,奇怪的字符不可见,仅在post请求的正文中。@Alex我明天必须测试它,我的ATM中没有服务器:/应该是:var idx=$('.input.file').length;
?size()
同样有效:。我应该切换到长度
,但这更多的是一个实验,而不是影响生产的东西,所以我没有太多注意:)。尝试复制但失败:你能更新那里的代码并复制你描述的吗?给你:。克隆元素时,奇怪的字符不可见,只有在post请求的正文中。@Alex我明天必须测试它,我没有服务器:/
<div class="file">
<div>
<label for="Attachment0File">File: </label>
<input type="file" name="data[Attachment][file][]" id="Attachment0File" />
</div>
</div>
<button type="button" id="new-image">New</button>
$(document).ready(function () {
$('#new-image').click(function (e) {
e.preventDefault();
var idx = $('.file').length;
$('.file:first-child').clone(true, true)
.find('label').attr('for', 'Attachment'+idx+'File').end()
.find('input[type="file"]').attr('name', 'data[Attachment][file][]').attr('id', 'Attachment'+idx+'File').end()
.insertBefore('#new-image');
});
});