表没有使用jQuery显示?

时间:2014-03-31 04:16:04

标签: jquery select html-table row

我有一个选择选项。当用户选择我想要将行追加到表中的选项时。例如,如果用户选择2,那么我想向表中添加2行,但我的行没有显示。

<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<p><strong>Select No of Activites You Want To Fill Up:</strong>
   <select id="noAct">
      <option value="1>1</option>
      <option value="2">2</option>
      <option value="3">3</option>
   </select>


    <table class="table table-bordered" id="tables">
        <tr>
            <th>SN</th>
            <th>Activity Description</th>
            <th>Unit</th>
            <th>Weightage</th>
            <th>100</th>
            <th>75</th>
            <th>50</th>
            <th><50</th>
        </tr>

    </table>
<script>
    function displayTable()
    {
        var value = $("#noAct").val();

        $("#tables").empty();
        //for loop to display the no of tables required
        for ( var i=0; i < value; i++ )
        {
            //display the table
            $("#tables").append("<tr>
                                                    <td>1</td>
                                                    <td>Web Site</td>
                                                    <td>Time</td>
                                                    <td>20</td>
                                                    <td>1 days</td>
                                                    <td>2 days</td>
                                                    <td>3 days</td>
                                                    <td>4 days</td>
                                                 </tr>

                ");

        }
    }

    $( "select" ).change( displayTable );
    displayTable();
</script>

2 个答案:

答案 0 :(得分:1)

试试这个

<强> HTML

<select id="noAct">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
   </select>


    <table  class="table table-bordered">
        <tr>
            <th>SN</th>
            <th>Activity Description</th>
            <th>Unit</th>
            <th>Weightage</th>
            <th>100</th>
            <th>75</th>
            <th>50</th>
            <th><50</th>
        </tr>
          <tbody id="tbodyappend">
                </tbody>
    </table>

<强> JS

$(document).ready(function(){

    function displayTable()
    {
       var value = $("#noAct").val();

      $("#tbodyappend").empty();
        //for loop to display the no of tables required
        for ( var i=0; i < value; i++ )
        {
            //display the table
            $("#tbodyappend").append('<tr><td>'+(i+1)+'</td><td>Web Site Maintenance</td><td>Time</td><td>20</td><td>1 days</td><td>2 days</td> <td>3 days</td><td>>3 days</td></tr>');

        } 
    }

        $( "select" ).change(displayTable);

});

<强> LIVE DEMO HERE

答案 1 :(得分:0)

试试这个:

function displayTable()
    {
       var value = $("#noAct").val();

      $("#tables").empty();
        //for loop to display the no of tables required
        for ( var i=0; i < value; i++ )
        {
            //display the table
            $("#tables").append('<td>1</td><td>Web Site Maintenance</td><td>Time</td><td>20</td><td>1 days</td><td>2 days</td> <td>3 days</td><td>>3 days</td>');

        } 
    }

        $( "select" ).change(displayTable);

live demo