Javascript 如何添加“输入类型”;文件";换一排?
当用户在表格中添加一行时,应在每行的“图像”列下显示输入“文件”功能,以便用户可以为每行选择一个文件。但当我添加一行时,它不会显示该行中的文件输入 以下是html代码:Javascript 如何添加“输入类型”;文件";换一排?,javascript,jquery,dom,Javascript,Jquery,Dom,当用户在表格中添加一行时,应在每行的“图像”列下显示输入“文件”功能,以便用户可以为每行选择一个文件。但当我添加一行时,它不会显示该行中的文件输入 以下是html代码: <table id="qandatbl" border="1"> <tr> <th class="image">Image</th> </tr> </table> 形象 下面是jquery代码 functi
<table id="qandatbl" border="1">
<tr>
<th class="image">Image</th>
</tr>
</table>
形象
下面是jquery代码
function insertQuestion(form) {
var $tr = $("<tr></tr>");
var $image = $("<td class='image'></td>");
function image(){
var $this = $(this);
var $imagefile = $("<input type='file' name='imageFile' id='imageFile' />").attr('name',$this.attr('name'))
.attr('value',$this.val())
$image.append($imagefile);
};
$tr.append($image);
$('#qandatbl').append($tr);
}
函数insertQuestion(form){
var$tr=$(“”);
变量$image=$(“”);
函数映像(){
var$this=$(this);
var$imagefile=$(“”).attr('name',$this.attr('name'))
.attr('value',$this.val())
$image.append($imagefile);
};
$tr.append($image);
$('qandabl')。追加($tr);
}
我在任何地方都看不到对内部函数image()
的调用。更多关于为什么你有这样的内在功能?解决这个问题很可能会解决您的问题。类似这样的问题,但我的问题是,这是什么?由于您当前生成的函数未在任何后续操作中使用,因此我当前删除了该函数,但无法确定$(this).val()
会产生什么结果
您还传递了一个表单
参数,但不使用它
function insertQuestion(form) {
var $tr = $("<tr></tr>");
var $image = $("<td class='image'></td>");
var $imagefile = $("<input type='file' name='imageFile' id='imageFile' />").attr('name',$this.attr('name'))
.attr('value',$(this).val())
.appendTo($image);
$tr.append($image);
$('#qandatbl').append($tr);
}
函数insertQuestion(form){
var$tr=$(“”);
变量$image=$(“”);
var$imagefile=$(“”).attr('name',$this.attr('name'))
.attr('value',$(this.val())
.appendTo($image);
$tr.append($image);
$('qandabl')。追加($tr);
}
您当前的代码片段看起来很混乱。试试这个:
形象
这个javascript:
function insertQuestion(form) {
var imageField = $('<input />')
.attr({
type: 'file',
name: 'imageFile',
id: 'imageFile'
});
$(form).find('table').append($('<tr />').append(imageField));
}
insertQuestion($('#idOfTheForm'));
函数插入问题(表单){
var imageField=$('')
艾特先生({
键入:“文件”,
名称:'imageFile',
id:'imageFile'
});
$(form).find('table').append($('').append(imageField));
}
插入问题($);
您喜欢PHP语法吗$变量在javascript中不是必需的variable
(不带$)就足够了。@Armin:当变量名表示jQuery对象时,在变量名前面加上$
是一种被广泛采用的惯例。噢!从未听说过这个大会,但很高兴知道;-)@阿明:嗯,好的。但这似乎是一个非官方创建的“约定”,因为在正式jquery文档中,所有示例都没有这个约定。
function insertQuestion(form) {
var imageField = $('<input />')
.attr({
type: 'file',
name: 'imageFile',
id: 'imageFile'
});
$(form).find('table').append($('<tr />').append(imageField));
}
insertQuestion($('#idOfTheForm'));