动态附加文本行 [英] Dynamically appending text lines
问题描述
代码的目标是每次最后一行被重点创建一个新的输入行,直到一定量的行。
我被提供了这个解决方案,这非常有用:
The goal of the code is to create a new input line each time the last line is focused, until a certain amount of lines. I was offered this solution which works great:
$(function() {
$("#qc>div:last-of-type>input").live('focus', function() {
$(this).parent().after($(this).parent().clone().attr('id', 'ansInput' + $('#qc>div').length).find('input').val('').end());
});
});
但我也想改变每一行之前的文字,所以克隆
不会剪切
but I also want to change each time the text before the line, so clone
won't cut it
我修改了它,以编写一个预先准备好的HTML行,但它不起作用:
I modified it to write a pre-ready HTML line, but it doesn't work:
function questionsForm() {
$("#qc>div:last-of-type>input").live('focus', function() {
lineNum = $('#qc>div').length
newLine = ("<div id='ansInput{0}'>Answer {0}: <input type='text' name='ans{0}' /></div><!--ans1-->").format(lineNum);
if ($('#qc>div').length <= 4) {
$(this).parent().after(newLine);
}
});
}
( .format
方法是预定义的)
我想了解为什么我的代码不工作,以及如何加以说明,但并不完全不同的解决方案(除非我的代码有一些根本的错误..)
I would like to understand why my code isn't working, and how to chamge it, but not completely different solutions (unless of course my code has some fundamental errors..)
推荐答案
在 lineNum = $('# qc> div')。length
为什么在questionForm()函数中的代码?
你可以将它放在jquery ready函数中 $(function(){...});
why is that code in the questionsForm() function?
you could just place it in the jquery ready function $(function(){ ... });
你的格式函数看起来像?
Also what does your format function look like?
这篇关于动态附加文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!