Javascript,将值传递到新添加的表行/单元格

时间:2019-02-12 05:56:51

标签: javascript php html

基本上,此请求的目的是要有一个模式表,该模式表将添加新的表行/单元格并将“ var OBGyneID = $(this).attr(“ id”);”的值传递给第一个单元格并赋值为“ var billing_id = newID;”每当添加新行/单元格时,都将移至第二个单元格。寻求帮助,谢谢!

*我的模态*

    <div id="myModal" class="modal fade card new-contact myModal" role="dialog">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">

                <div class="card profile">
                    <div class="profile__img">
                        <img src="img/profile2.jpg" alt="">
                        <a href="#" class="zmdi zmdi-camera profile__img__edit"></a>
                    </div>
                    <div class="profile__info">                                         
                        <h3 style="text-transform:capitalize;" id="pxname" name="pxname"></h3>
                        <ul class="icon-list">
                            <li>LAST <p name="consdates" id="consdates"></p></li>
                            <li>Last visit: </li>
                            <li><i class="zmdi zmdi-twitter"></i> @mallinda-hollaway</li>
                        </ul>
                    </div>
                </div>              
                <div>
                    <div class="table-responsive">
                        <table id="billingTable" class="table table-inverse table-sm table-hover table-condensed" style="font-size:120%">
                            <thead> 
                                <th>OBDYID</th>
                                <th>CK</th>
                                <th>PROCEEDURE</th>
                                <th>AMOUNT</th>
                            </thead>
                            <tbody id="billing_body" style="text-transform:capitalize;">    
                                <form method="get" id="insert_form">                
                                    <tr>
                                        <td width="10%"><input type="text" style="width:100%;" id="OBGyneID" name="OBGyneID"></td>
                                        <td width="10%"><input type="text" style="width:100%;" id="AssessMentEntryID" name="AssessMentEntryID"></td>
                                        <td width="60%"><input type="text" style="width:100%;" class="ExamDesc" type="text" id="ExamDesc" name="ExamDesc"></td>
                                        <td width="20%"><input type="text" style="width:100%;" class="Price" type="text" id="Price" name="Price"></td>                              
                                    </tr>
                                </form>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />
                    <input type="submit" id="addRow" value="add" name="add" class="btn btn-info" />             
                    <button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

* MY JS添加新行/单元格*

        $(document).ready(function () {
            $("#addRow").click(function () {
                $("#billingTable").append('<tr><td width="10%"><input type="text" style="width:100%;" id="OBGyneID" name="OBGyneID" ></td>'+
                            '<td width="10%"><input type="text" style="width:100%;" id="AssessMentEntryID" name="AssessMentEntryID"></td>'+
                            '<td width="60%"><input type="text" style="width:100%;" class="ExamDesc" type="text" id="ExamDesc" name="ExamDesc"></td>'+
                            '<td width="20%"><input type="text" style="width:100%;" class="Price" type="text" id="Price" name="Price"></td></tr>');

                jQuery(".ExamDesc").autocomplete({
                    source: 'php/billingentry.php'
                }); 
            });
        });

*将值传递给Modal * ,这仅在模式弹出窗口时传递值,而每当添加新的行/单元格时都无法传递值。

            $(document).on('click', '.billing_data', function(){  
            var OBGyneID = $(this).attr("id");  
            var newID = new Date().getUTCMilliseconds();
            var billing_id = newID;
            $.ajax({  
                    url:"php/billing_fetch.php",  
                    method:"GET",  
                    data:{OBGyneID: OBGyneID},  
                    dataType:"json",  
                    success:function(data){
                        $('#AssessMentEntryID').val(data.AssessMentEntryID); 
                        $('#ExamDesc').val(data.ExamDesc);  
                        $('#Price').val(data.Price);  

                        $('#pxservice').val(data.pxservice);
                        $('#companyname').val(data.companyname);
                        $('#chiefcomplain').val(data.chiefcomplain);

                        document.getElementById("OBGyneID").value = OBGyneID;
                        document.getElementById("AssessMentEntryID").value = billing_id;
                        document.getElementById("pxname").innerHTML = (data.fname)+" "+(data.mi)+" "+(data.lname);
                        document.getElementById("consdates").innerHTML = (data.obgyneDate);
                        $('#myModal').modal('show');
                    }  
            });  
        });

1 个答案:

答案 0 :(得分:0)

每当在结算表中添加新行HTML时,OBGyneID,AssessMentEntryID,ExamDesc和Price都使用相同的ID。而且,如果添加了两行,则DOM将无法正常工作,因为那里有多个相同的ID。因此,我建议您将其替换为class并使用class name设置值。检查以下代码:

$(document).ready(function () {
    $("#addRow").click(function () {
        $("#billingTable").append('<tr><td width="10%"><input type="text" style="width:100%;" class="OBGyneID" name="OBGyneID" ></td>'+
                    '<td width="10%"><input type="text" style="width:100%;" class="AssessMentEntryID" name="AssessMentEntryID"></td>'+
                    '<td width="60%"><input type="text" style="width:100%;" class="ExamDesc" type="text" name="ExamDesc"></td>'+
                    '<td width="20%"><input type="text" style="width:100%;" class="Price" type="text" name="Price"></td></tr>');

        jQuery(".ExamDesc").autocomplete({
            source: 'php/billingentry.php'
        }); 
    });
});

您已经添加了HTML代码,然后可以找到billingTable的最后一行,并可以在其中设置值。检查以下代码:

$(document).on('click', '.billing_data', function(){  
    var OBGyneID = $(this).attr("id");  
    var newID = new Date().getUTCMilliseconds();
    var billing_id = newID;
    $.ajax({  
        url:"php/billing_fetch.php",  
        method:"GET",  
        data:{OBGyneID: OBGyneID},  
        dataType:"json",  
        success:function(data){

            $("#billingTable").find(".AssessMentEntryID:last").val(data.AssessMentEntryID);
            $("#billingTable").find(".ExamDesc:last").val(data.ExamDesc);
            $("#Price").find(".Price:last").val(data.Price);
            $("#billingTable").find(".consdates:last").html(data.obgyneDate);
            //and so on
            $('#myModal').modal('show');
        }  
    });  
});

此外,我建议您使用HTMl模板标签对行进行克隆。不建议在JS中附加HTML。希望对您有帮助。