[英]generate n number of table rows depending on value of input jquery
<input type="number" id="nostorey" name="" class=' InputBox' />
<table id="floor">
<tr id="headtable">
<td>
<center>Floor Names</center>
</td>
<td>
<center>Floor wise Area</center>
</td>
</tr>
<tr>
<td>
<p>1st Floor</p>
</td>
<td>
<input type='text' id="firstfloor" name='' maxlength="10" value="" class=' InputBox' />
</td>
</tr>
<tr>
<td>
<p>2nd Floor</p>
</td>
<td>
<input type='text' id="secondfloor" name='' maxlength="10" value="" class=' InputBox' />
</td>
</tr>
<tr>
<td>
<p>3rd Floor</p>
</td>
<td>
<input type='text' id="thirdfloor" name='' maxlength="10" value="" class=' InputBox' />
</td>
</tr>
<tr>
<td>
<p>4th Floor</p>
</td>
<td>
<input type='text' id="fourthfloor" name='' maxlength="10" value="" class=' InputBox' />
</td>
</tr>
<tr>
<td>
<p>Total</p>
</td>
<td>
<input type='text' id="total" readonly name='' maxlength="10" value="" class=' InputBoxD' />
</td>
</tr>
</table>
$("#nostorey").bind('change', function() {
if ($.trim($(this).val()) < 5) {
if ($(this).val().match(/^\d*$/)) {
if ($(this).val() == 1) {
console.log("1");
console.log($(this).val());
$('#secondfloor').prop('disabled', true);
$('#thirdfloor').prop('disabled', true);
$('#fourthfloor').prop('disabled', true);
} else if ($(this).val() == 2) {
console.log("2");
console.log($(this).val());
$('#secondfloor').prop('disabled', false);
$('#thirdfloor').prop('disabled', true);
$('#fourthfloor').prop('disabled', true);
} else if ($(this).val() == 3) {
console.log("3");
console.log($(this).val());
$('#secondfloor').prop('disabled', false);
$('#thirdfloor').prop('disabled', false);
$('#fourthfloor').prop('disabled', true);
} else if ($(this).val() == 4) {
console.log("4");
console.log($(this).val());
$('#secondfloor').prop('disabled', false);
$('#thirdfloor').prop('disabled', false);
$('#fourthfloor').prop('disabled', false);
}
}
} else {
var newItemHTML = '<tr><td ><span>' + $(this).val() + 'th Floor</span></td><td><input type="text" name="" class="InputBox " id="floor' + $(this).val() + '"></td></tr>';
$("table#floor tr").last().before(newItemHTML);
}
});
This is my code to tell how many floor I have in my input text by default I have 4 floors. 这是我的代码,用于告诉我默认情况下在输入文本中有几层楼。
Onchange
of onstorey input I want to add the remaining floors currently what i did is to set if else but this is not working the way i want it because this way if I reduce the number of floor it is not reducing the number of input to write the area. Onchange
输入的更改我想添加当前剩余的楼层,如果没有,这是我要设置的,但这不能按我想要的方式工作,因为这样,如果我减少楼层数,就不会减少要写入的输入数该地区。 I want to ask idea on how to make this possible 我想问一下如何做到这一点的想法
When the number is reduced the number of input in the table should also decrease, but not less than the default value which is 4 减少数量时,表中的输入数量也应减少,但不得少于默认值4
see your updated fiddle: http://jsfiddle.net/fq42seff/3/ 查看更新的小提琴: http : //jsfiddle.net/fq42seff/3/
i first added the class="floor"
to all your floor input boxes, to have a unique selector for these input boxes. 我首先将
class="floor"
到所有楼层输入框,以便为这些输入框提供唯一的选择器。 the entry field for the amount of floors and the total field is excluded. 楼层数和总数字段的输入字段被排除。
then i changed your js the following: 然后我将您的js更改为以下内容:
//created two functions addFloors() and removeFloors()
function addFloors(actual, target){
for(i = actual +1;i<=target;i++) //this loop creates the new floors
{
newItemHTML = '<tr><td ><p>' + i + 'th Floor</p></td><td><input type="text" name="" class="floor InputBox " id="floor' + i + '"></td></tr>';
//i also changed the html inside the first td from <span> to <p> to match your html markup
$("table#floor tr").last().before(newItemHTML);
}
}
function removeFloors(target){
if(target >= 4) //remove all floors except the base 4
{
$('.floor').slice(target).parent().parent().remove();
//since i select the .floor input box, i have to use the parent() function two times, to move the selector up to the <tr> element
}
}
next, we extend your change function: 接下来,我们扩展您的更改功能:
$("#nostorey").bind('change', function() {
curVal = $.trim($(this).val()).match(/^\d*$/); //get the value from the first input box
curFloors = $('.floor').length; //get the current nbr of floors
if(curVal > curFloors) //if you want more floors, then currently available
{
addFloors(curFloors, curVal); //add floors
}else if(curVal < curFloors) //if you want less
{
removeFloors(curVal); //remnove them
}
last but not least, enable/disable the first 4 input boxes: 最后但同样重要的是,启用/禁用前四个输入框:
$('.floor').each(function(index){ //for each .floor input box
if(index >= curVal) //if it's index is greater then the needed floor count
{
$(this).prop('disabled', true); //disable it
}else
{
$(this).prop('disabled', false); //else enable it
}
});
the last part - the enabling/disabling could be splitted and extend the add/remove functions - this would make them get run only when needed. 最后一部分-启用/禁用可以拆分,并扩展添加/删除功能-这将使它们仅在需要时运行。 right now, it gets executed on every value change.
现在,它会在每次值更改时执行。 but i guess, you can figure out the rest by yourself...
但我想,您可以自己找出其余的...
I added a grid of checkboxes depending on the number of floors also upon generating these checkboxes i put an attribute for each checkboxes depending on which row they are in. The span text or that row will be the value of the checkboxes for that row. 在生成这些复选框时,我还根据楼层数添加了一个复选框网格,并根据它们所在的行为每个复选框设置了一个属性。跨度文本或该行将是该行的复选框的值。 With the help of
在...的帮助下
Guruprasad Rao
he came up with this fiddle 他想出了这个小提琴
For code betterment feel free to update the fiddle to help others 为了使代码更好,请随时更新小提琴以帮助他人
Correct me if I am wrong. 如果我错了,请纠正我。 Here is my for loop.
这是我的for循环。
else { var floors = parseInt($("#nostorey").val()-4); $("tr[id^=floor]").hide(); if(floors != NaN){ for(i=5;i<floors+5 ;i++){ var newItemHTML = '<tr id="floor'+i+'"><td ><span>' + i + 'th Floor</span></td><td><input type="text" name="" class="InputBox floor"' + i + '"></td></tr>'; $("table#floor tr").last().before(newItemHTML); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.