JAVASCRIPT:动态表行,具有自动生成的选定值组合

时间:2017-04-07 02:52:14

标签: javascript html css twitter-bootstrap-3

我在 SELECT: Combine value of two or more select and get their combination as output 上提出我的第一个问题。现在,在谷歌搜索后,我发现动态表行创建和删除,我用这个问题。经过一个小时的编纂和分析,我得到了这个。我现在的问题是如下所示:
1。当我只有1个选择而我选择一个选项没有任何反应时,我会在添加包含另一个选择的新行时工作。
2. 当我删除1行时,输入框中的值未删除 3. 我只能删除最后一行,可以选择吗?

以下代码如下。我希望你能帮助我。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta http-equiv="Cache-control" content="no-cache"> <!--The No-Cache-->
    <title>Elements Combination</title>

    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

    <body>
        <div class="container">
            <div class="row clearfix">
                <div class="col-md-12 column">
                    <table class="table table-bordered table-hover" id="tab_logic">
                        <thead>
                            <tr >
                                <th class="text-center">
                                    #
                                </th>
                                <th class="text-center">
                                    Elements
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr id='addr0'>
                                <td>
                                1
                                </td>
                                <td>
                                    <select class="form-control" name='name0' >
                                      <option value="H">Hydrogen</option>
                                      <option value="He">Helium</option>
                                      <option value="Be">Beryllium</option>
                                      <option value="B">Boron</option>
                                      <option value="C">Carbon</option>
                                      <option value="N">Nitrogen</option>
                                    </select>
                                </td>
                            </tr>
                            <tr id='addr1'></tr>
                        </tbody>
                    </table>
                    <input type="text" class="form-control" placeholder="Combination Here" name="combination" id="combination" readonly="">
                </div>
            </div>
            <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
        </div>
  </body>
</html>

<script type="text/javascript">
    $(document).ready(function(){
        var i=1;

        $("#add_row").click(function(){
            $('#addr'+i).html("<td>"+ (i+1) +"</td><td><select class='form-control' name='name"+i+"' ><option value='H'>Hydrogen</option><option value='He'>Helium</option><option value='Be'>Beryllium</option><option value='B'>Boron</option><option value='C'>Carbon</option><option value='N'>Nitrogen</option></select></td>");

            $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
            i++; 

            $('select').on('change', function(){

                var s = '';

                $('select').each(function(){
                    if(s == '') {
                        s = $(this).val();
                    } else {
                        s = s + ' + ' + $(this).val() ;  
                    }
                })
                $('#combination').val(s);
            })

        });
        $("#delete_row").click(function(){
            if(i>1){
                $("#addr"+(i-1)).html('');
                i--;
            }
        });
    });
</script>

2 个答案:

答案 0 :(得分:1)

考虑使用可重用的功能,这将有助于您的代码更加模块化。你可以在每一行上放一个删除行功能。

请参阅此处的示例:https://jsfiddle.net/dcpcq56v/

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta http-equiv="Cache-control" content="no-cache"> <!--The No-Cache-->
    <title>Elements Combination</title>

    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

    <body>
<div class="container">
  <div class="row clearfix">
    <div class="col-md-12 column">
      <table class="table table-bordered table-hover" id="tab_logic">
        <thead>
          <tr >
            <th class="text-center">
              #
            </th>
            <th class="text-center">
              Elements
            </th>
            <th class="text-center" style="width:20%;">
            </th>
          </tr>
        </thead>
        <tbody id='element_table'>

        </tbody>
      </table>
      <input type="text" class="form-control" placeholder="Combination Here" name="combination" id="combination" readonly="">
      <a id="add_row" class="btn btn-default pull-left">Add Row</a>
    </div>
  </div>
</div>
  </body>
</html>

<script type="text/javascript">
    $(document).ready(function(){
  var select_item = function(){
    $('#combination').val( 
        $("#element_table select").map(function(index, element){ return $(element).val(); }).get().join("+")
    );
  };
  var add_row = function(){
    var new_row = $("<tr><td>"+ ($("#element_table>tr").length+1) +"</td><td><select class='form-control'><option value='H'>Hydrogen</option><option value='He'>Helium</option><option value='Be'>Beryllium</option><option value='B'>Boron</option><option value='C'>Carbon</option><option value='N'>Nitrogen</option></select></td><td><a id='delete_row' class='pull-right btn btn-default'>Delete Row</a></td></tr>").appendTo("#element_table");

    select_item();
    new_row.find('select').on('change', select_item);
    new_row.find("#delete_row").click(function(){ 
        $(this).parents("tr").remove(); 
      select_item(); 
    });
  };

  add_row();

  $("#add_row").click(add_row);

});
</script>

答案 1 :(得分:0)

您可以在每个<td>中创建一些带有行数的删除图标。

$(deleteIcon).click(function () 
    var td = $(this).parent()[0];
    $(td).parent()[0].remove();
});