递增克隆行中输入字段的值

时间:2013-08-27 17:36:05

标签: javascript jsp servlets

1)这里我正在克隆一行......但是这段代码只在eclipse中工作[即,克隆工作]并且它也无法在任何浏览器中工作。

2)获取具有相同名称的克隆行中文本框的值的解决方案是什么,并使用jsp和servlet插入到数据库中? 我怎样才能获得具有相同名称的那些值

3)我有servlet代码只从jsp

获取单个值
    String address_seq_num =request.getParameter("address_seq_num");

how can i get the value of address seq number in the cloned row fromjsp to servlet to insert into the next row of a table in the database.

4)如果我在这段代码中提到“DOCUMENT TYPE”,它在eclipse中也行不通..... 请指导我......

的JavaScript

function clonetable() {
 var x=document.getElementById("main_table"); //get the table 
var rowCount = x.rows.length; 
var row = document.getElementById("table_row_clone"); // find row to copy
var table = document.getElementById("table_body"); // find table to append to 
var clone = row.cloneNode(true); // copy children too 

var tb1 = clone.document.getElementById("asn");//here i'm incrementing the value     
tb1.value=rowCount+1;//of "address seq num " in the cloned row


    clone.id = "abc"; // change id or other attributes/contents 
    table.appendChild(clone); // add new row to end of table 
}

function deltable() {
    var x = document.getElementById("main_table"); //get the table 
    var rowCount = x.rows.length;
    if (rowCount > 1) {
        x.deleteRow(rowCount - 1);
    } //delete the last row 
}

HTML

<table id="main_table" align="center" style="width:75%">
    <tbody id="table_body">
        <tr id="table_row_clone">
            <td>
                <table align="center" style="width:100%">
                    <tr>
                        <td align="center">
                            <div style="border:3px solid silver;border-radius:5px;background-color:grey">
                                <table width="100%">
                                    <tr>
                                        <th align="center">Address Details</th>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="border:3px solid silver;border-radius:5px;background-color:#1E90FF">
                                <table align="center" style="width:99%">
                                    <tr style="background-color:#1E90FF">
                                        <td style="width:35%">
                                            <table width="100%">
                                                <tr id="slrow">
                                                    <td style="width:43%">Address Seq Num</td>
                                                    <td>
                                                        <input id="asn" style="width:60px" name="address_seq_num" type="text" value="1" readonly>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                        <td width="49%" align="right">
                                            <input style="width:80%" type="text" value="Reg.office/Primary Office">
                                        </td>
                                        <td align="right">
                                            <input style="width:30px" type="button" value="+" onclick="clonetable()">
                                            <input style="width:30px" type="button" value="-" onclick="deltable()">
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </tbody>

1 个答案:

答案 0 :(得分:0)

根据您的HTML(顺便说一句,这很麻烦)您可以使用以下内容增加该文本框的值:

var tb = document.getElementById("asn");
tb.value = parseInt(tb.value, 10) + 1;

技巧是你必须将文本框的值转换为数字,这就是parseInt在该示例中所做的。

请注意,如果值不是有效数字,上面的代码段会在文本框中显示“NaN” - 它根本不会进行任何数据验证。