开发者

jquery adding mutiple table column base on given number

开发者 https://www.devze.com 2023-02-22 22:29 出处:网络
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\"/>

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>";
0

精彩评论

暂无评论...
验证码 换一张
取 消