简体   繁体   English

用jquery和for函数生成的输入元素

[英]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: 输出将如下所示:

  • Input 1 输入1
  • Input 2 输入2
  • Input 3 输入3
  • Input 4 输入4
  • Input 5 输入5
  • Input 6 输入6

From the code above how to get output like this (Add xxx every after 2 row): 从上面的代码中,如何获取这样的输出(在每2行之后添加xxx):

  • Input 1 输入1
  • Input 2 输入2
  • xxx xxx
  • Input 3 输入3
  • Input 4 输入4
  • xxx xxx
  • Input 5 输入5
  • Input 6 输入6

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: 它测试两件事:

  1. Don't put "xxx" to the end 不要在结尾加上“ xxx”
  2. The remainder of i/2 is 1 i / 2的余数是1

Demo jsfiddle HERE . 演示jsfiddle在这里 Demo with variable xxx step size HERE 此处具有可变xxx步长的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM