【问题标题】:incrementing the value of a input field in the cloned row增加克隆行中输入字段的值
【发布时间】:2013-08-27 17:36:05
【问题描述】:

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)如果我在这段代码中提到“文档类型”,它在 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>

【问题讨论】:

  • 你在哪里递增?我在你的 JS 代码中没有看到它
  • 你的 HTML 标记很乱,很多元素还没有关闭。我建议。您发布了一个更干净的 html 标记,并正确关闭和结束元素标签。
  • 感谢您的回复...实际上在此代码中,我想在克隆后在下一行中增加“地址序列号”...目前它的值为“1”...但我不知道该怎么做....我试过这个代码 var tb = document.getElementById("asn"); tb.value = parseInt(tb.value, 10) + 1;它会增加值,但它也会在连续添加时更改第一行值...

标签: javascript jsp servlets


【解决方案1】:

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

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

诀窍是您必须将文本框的值转换为一个数字,这就是 parseInt 在该示例中所做的。

请注意,如果值不是有效数字,上述 sn-p 将在文本框中为您提供“NaN” - 它根本不进行任何数据验证。

【讨论】:

  • 感谢您的回复....我尝试了此代码...它增加了值,但它也会在连续添加时更改第一行的值...
  • 是的,这意味着你有相同的 id 在你的行中传播。然后,您需要以某种方式唯一标识该行。换句话说,document.getElementById("asn") 每次只是抓取同一个文本框,因为它是页面上第一个 ID 为“asn”的元素。
  • 如果我在这段代码中写 ,它在 Eclipse 中也不起作用。 ..为什么?
  • 请转到此链接...我将图像粘贴到此处。请指导我。 quora.com/JavaScript/…
猜你喜欢
  • 2015-09-28
  • 1970-01-01
  • 2015-05-23
  • 2021-10-19
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
相关资源
最近更新 更多