[英]Generated input element with jquery and for function
This code to generate input element with jquery and the amount of element is calculated from times of two other input 此代码使用jquery生成输入元素,并且元素的数量是从另外两个输入的时间计算得出的
$('#generate').click(function () {
var a = parseInt($('#row').val());
var b = parseInt($('#col').val());
var val = a*b;
var innerhtml = '';
for (var i = 0; i < val; i++) {
innerhtml += "<input type='text' class='seat' id='" + (i + 1) + "' name='" + (i + 1) + "'>";
for (var j = 0; j = a; j++) {
innerhtml += "xxx";
}
}
$('#textbox_div').html(innerhtml);
});
If i fill input #row and #col with 2 & 3, jquery will generated 6 element (2*3 = 6). 如果我用2和3填充输入#row和#col,jquery将生成6个元素(2 * 3 = 6)。 The output will be like this: 输出将如下所示:
From the code above how to get output like this (Add xxx every after 2 row): 从上面的代码中,如何获取这样的输出(在每2行之后添加xxx):
Like this: 像这样:
var a = 4; //parseInt($('#row').val());
var b = 2; //parseInt($('#col').val());
var val = a*b;
var innerhtml = '';
for (var i = 0; i < val; i++) {
innerhtml += "<input type='text' class='seat' id='" + (i + 1) + "' name='" + (i + 1) + "'>";
if (i < val - 1 && i % 2 === 1 ) {
innerhtml += "xxx";
}
}
$('#textbox_div').html(innerhtml);
As you can see I replaced your inner for
loop to an if
statement. 如您所见,我将内部for
循环替换for
if
语句。 It tests two things: 它测试两件事:
Demo jsfiddle HERE . 演示jsfiddle在这里 。 Demo with variable xxx step size HERE 此处具有可变xxx步长的演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.