i want to add table base on the input number it supposed to be like this: value=3
<input type="text" id="pila" name="pila" maxlength="2px" onchange="balhin()" value="3"/>
js function:
function balhin(){
var pila = $("#pila").val();
var a;
var i = 0;
while (i <= pila)
{
a .= "<tr><td>amew</td><td>amew</td><td>amew</td></tr>";
i++;
}
$(".pakita").append(a);
}
the html code:
<table id="tablesorter">
<thead>
<tr>
<th>#</th>
<th>Trip Number</th>
<th>NO. Boxes</th>
</tr>
</thead>
<tbody class="pakita"></tbody>
</table>
but the js function is not working? what would be the possible reason why its not working?
expected output:
<table id="tablesorter">
<thead>
<tr>
<th>#</开发者_如何学JAVAth>
<th>Trip Number</th>
<th>NO. Boxes</th>
</tr>
</thead>
<tbody class="pakita">
<tr><td>amew</td><td>amew</td><td>amew</td></tr>
<tr><td>amew</td><td>amew</td><td>amew</td></tr>
<tr><td>amew</td><td>amew</td><td>amew</td></tr>
</tbody>
</table>
a .= "<tr><td>amew</td><td>amew</td><td>amew</td></tr>";
seems incorrect. javascript concatenation is done by +=
a += "<tr><td>amew</td><td>amew</td><td>amew</td></tr>";
also initialize a="" ;
(empty string)
.=
is not a valid operator in JavaScript. Instead, use +=
:
function balhin(){
var pila = $("#pila").val();
var a;
var i = 0;
while (i <= pila)
{
a += "<tr><td>amew</td><td>amew</td><td>amew</td></tr>";
i++;
}
$(".pakita").append(a);
}
Change your code to this:
var a = "";
var i = 0;
while (i <= pila)
{
a += "<tr><td>amew</td><td>amew</td><td>amew</td></tr>";
i++;
}
I'm not 100% sure but string concatenation in javascript is done by '+' not '.'
var a = "";
a += "<tr><td>amew</td><td>amew</td><td>amew</td></tr>";
精彩评论