【问题标题】:Table not being displayed using jQuery?没有使用 jQuery 显示表格?
【发布时间】:2014-05-10 07:38:58
【问题描述】:

我有一个选择选项。当用户选择我想将该行附加到表中的选项时。例如,如果用户选择 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>

【问题讨论】:

    标签: jquery select html-table row


    【解决方案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

    【讨论】:

      【解决方案2】:

      试试这个:

      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

      【讨论】:

      • 刚改了还是不显示!!
      • 我在那里找不到解决方案。能不能具体点!!
      • 你在演示中测试过吗?你不需要改变html。您在 jquery 中有额外的函数调用,只需尝试我的 jquery。它应该可以工作。
      猜你喜欢
      • 2013-02-04
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多