0

我正在尝试创建一个模式,其中包含一个表,该表具有来自选择选项的指定数量的行。这是我拥有的html代码。

      <div class ="form-group">
            <label for="RiskFields">How many fields will be needed for the Risk Assesment? <br> </label>
                <select id="RiskFields">
                    <option value = "5">5</option>
                    <option value = "6">6</option>
                    <option value = "7">7</option>
                    <option value = "8">8</option>
                    <option value = "9">9</option>
                    <option value = "10">10</option>
                </select>
                <p></p>
            <button type="button" class="btn btn-info btn-sm"  data-toggle="modal" data-target="#myModal" id = "createFields">Create Fields</button>

            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">

                  <!-- Modal content-->
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                      <h4 class="modal-title">Fields</h4>
                        </div>
                        <div class="modal-body">         
                         <table class = "table-bordered" width = "500" id = "fieldTables" >
                            <p> Input the Fields </p>
                            <p> The total score is out of 5 </p>
                            <thead>
                                <tr>
                                    <td width ="5%" data-field = "numField"> Field # </td>
                                    <td width ="20%" class = "test-center" dataField = "scoreBegin">Score</td>
                                    <td width="15%" class="text-center" data-field = "scoreEnd">Score</td>
                                    <td width="60%" class="text-center" data-field = "options" >options
                                    </td>
                                </tr> 


                            </thead>
                            <tbody>
                                 <tr>
                                    <td width ="5%" id = "numFirstField"> 1 </td>
                                    <td width ="20%" class = "text-center" id = "scoreNumBegin" >Score's from <br> 0</td>
                                    <td width="15%" class="text-center" id = "scoreNumEnd">To <input type = "text" class = "form-control" name = "firstScoreEnd">  </td>
                                    <td width="60%" class="text-center" id = "options" >
                                        <label for = "optionList">Choose from the following</label>
                                            <select class="form-control" multiple = "multiple" name = "options0" id = "optionList0">
                                                <option>Do Nothing</option>
                                                <option>Send to Watchlist</option>
                                                <option> do Something </option>
                                            </select>
                                    </td>
                                </tr>

                            </tbody>
                            <tfoot>
                                <tr>
                                    <td width ="5%" data-field = "numFieldLast">  </td>
                                    <td width ="20%" class = "text-center" datafield = "scoreBegin" >Score's from <input type = "text" class = "form-control" data-field = "score1"></td>
                                    <td width="15%" class="text-center" data-field = "scoreEnd">To <br> 5  </td>
                                    <td width="60%" class="text-center" data-field = "options" >
                                        <label for = "optionList">Choose from the following</label>
                                            <select class="form-control" id="optionList5" multiple = "multiple">
                                               <option>Do Nothing</option>
                                                <option>Send to Watchlist</option>
                                                <option> do Something </option>
                                            </select>

                                    </td>
                                </tr>
                            </tfoot>        
                        </table>    
                        </div>
                        <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                  </div>

                </div>
              </div>
            </div>

我希望应用程序做的是我希望它根据选择的数量创建表 - 2 行,因为已经创建了第一行和最后一行。所以我希望将选定的字段或行添加到 tbody 中的第一个字段之后。我正在查看此代码作为参考,但它还没有工作。这是我用来创建它的javascript。

<script>
    $(document).ready(function(){
      var numFields = parseInt(document.getElementById(('#RiskFields').value);
      $("#createFields").click(function(){
         $for(var i = 1; i < numFields ; i++){
            if (i == 1 ){
                break;
            }
            else{
                var row = ("<tr><td>"+ (i+1) +"</td><td><input name='scoreBeg"+i+"' type='text' class='form-control input-md'> </td><td><input  name='scoreEnd"+i+"' type='text'  class='form-control input-md'></td><td><label for ='options" + i + "'>Selectfrom the following</label><select class = 'form-control' multiple = 'multiple' name = 'options "+i + "' id = 'optionList" + i +"'><option>Do Nothing</option> <option>Send to Watchlist</option><option>dosomething</option></select></td>");

                        ("#fieldTables > tbody").append(row);
            }    
        }
    });            
  });
</script> 

是不是因为我试图以模态创建它,所以它有问题。如果您有任何建议,请告诉我。我已经尝试解决这个问题几个小时了,我开始拔头发。非常感谢。

4

1 回答 1

1

        function createFields() {
            var numFields = $("#RiskFields option:selected").val(); // parseInt(document.getElementById(('#RiskFields').value));
            for (var i = 1; i < numFields; i++) {
                if (numFields == 1) {
                    break;
                }
                else {
                    var row = ("<tr><td>" + (i + 1) + "</td><td><input name='scoreBeg" + i + "' type='text' class='form-control input-md'> </td><td><input  name='scoreEnd" + i + "' type='text'  class='form-control input-md'></td><td><label for ='options" + i + "'>Selectfrom the following</label><select class = 'form-control' multiple = 'multiple' name = 'options " + i + "' id = 'optionList" + i + "'><option>Do Nothing</option> <option>Send to Watchlist</option><option>dosomething</option></select></td>");

                    $("#fieldTables tbody").append(row);
                }
            }
        }

 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="form-group">
        <label for="RiskFields">
            How many fields will be needed for the Risk Assesment?
            <br>
        </label>
        <select id="RiskFields">
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
        <p>
        </p>
        <a type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal"
            onclick="createFields()" id="createFields">Create Fields</a>
        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            &times;</button>
                        <h4 class="modal-title">
                            Fields</h4>
                    </div>
                    <div class="modal-body">
                        <table class="table-bordered" width="500" id="fieldTables">
                            <p>
                                Input the Fields
                            </p>
                            <p>
                                The total score is out of 5
                            </p>
                            <thead>
                                <tr>
                                    <td width="5%" data-field="numField">
                                        Field #
                                    </td>
                                    <td width="20%" class="test-center" datafield="scoreBegin">
                                        Score
                                    </td>
                                    <td width="15%" class="text-center" data-field="scoreEnd">
                                        Score
                                    </td>
                                    <td width="60%" class="text-center" data-field="options">
                                        options
                                    </td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td width="5%" id="numFirstField">
                                        1
                                    </td>
                                    <td width="20%" class="text-center" id="scoreNumBegin">
                                        Score's from
                                        <br>
                                        0
                                    </td>
                                    <td width="15%" class="text-center" id="scoreNumEnd">
                                        To
                                        <input type="text" class="form-control" name="firstScoreEnd">
                                    </td>
                                    <td width="60%" class="text-center" id="options">
                                        <label for="optionList">
                                            Choose from the following</label>
                                        <select class="form-control" multiple="multiple" name="options0" id="optionList0">
                                            <option>Do Nothing</option>
                                            <option>Send to Watchlist</option>
                                            <option>do Something </option>
                                        </select>
                                    </td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td width="5%" data-field="numFieldLast">
                                    </td>
                                    <td width="20%" class="text-center" datafield="scoreBegin">
                                        Score's from
                                        <input type="text" class="form-control" data-field="score1">
                                    </td>
                                    <td width="15%" class="text-center" data-field="scoreEnd">
                                        To
                                        <br>
                                        5
                                    </td>
                                    <td width="60%" class="text-center" data-field="options">
                                        <label for="optionList">
                                            Choose from the following</label>
                                        <select class="form-control" id="optionList5" multiple="multiple">
                                            <option>Do Nothing</option>
                                            <option>Send to Watchlist</option>
                                            <option>do Something </option>
                                        </select>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">
                            Close</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

在您的代码中,onclick 函数和 data-target 都是不可能的。将其更改为“锚标记”。它的效果很好。以及您的 jquery 中的一些错误。在这个 "("#fieldTables tbody").append(row)" $ 中没有给出。在确切的代码和 html 下方删除 document.ready

    function createFields() {
        var numFields = $("#RiskFields option:selected").val(); // parseInt(document.getElementById(('#RiskFields').value));
        for (var i = 1; i < numFields; i++) {
            if (numFields == 1) {
                break;
            }
            else {
                var row = ("<tr><td>" + (i + 1) + "</td><td><input name='scoreBeg" + i + "' type='text' class='form-control input-md'> </td><td><input  name='scoreEnd" + i + "' type='text'  class='form-control input-md'></td><td><label for ='options" + i + "'>Selectfrom the following</label><select class = 'form-control' multiple = 'multiple' name = 'options " + i + "' id = 'optionList" + i + "'><option>Do Nothing</option> <option>Send to Watchlist</option><option>dosomething</option></select></td>");

                $("#fieldTables tbody").append(row);
            }
        }
    }


</script>
于 2015-08-14T05:10:36.020 回答