简体   繁体   English

如何使用 javascript 添加动态创建的文本框值

[英]how to add the dynamically created textbox values using javascript

please someone help out from this headaround problem of me;请有人帮助我解决这个棘手的问题;

I have dynamically created rows using javascript.Now my task is to add the textbox values.我已经使用 javascript 动态创建了行。现在我的任务是添加文本框值。

Since it is dynamically created,all the textbox is having only single id or name or class.由于它是动态创建的,所有文本框都只有一个 ID 或名称或 class。

I have one fixed textbox out of this table.When the user enters the value of the first textbox of firstrow it should enter into that fixed textbox.我在这个表中有一个固定的文本框。当用户输入 firstrow 的第一个文本框的值时,它应该输入那个固定的文本框。

Then, if he entered the values in the dynamically created second textbox (all the textbox will have same id's only) then both the textbox values should be added and get displayed in that fixed textbox on key press.然后,如果他在动态创建的第二个文本框中输入值(所有文本框将只有相同的 id),那么两个文本框值都应添加并在按键时显示在该固定文本框中。

Hope you might understood my problem.希望你能理解我的问题。

plz help me out.give me some sample codes if possible.moreover my values will be in the format of something like 12:00,09:00...请帮帮我。如果可能的话,给我一些示例代码。此外,我的值将采用 12:00,09:00 之类的格式...

first i need the solutions for that same id..how to resolve this id problem so that i can manage..help me首先,我需要相同 id 的解决方案。如何解决这个 id 问题,以便我可以管理..帮助我

If I am getting you correctly you want to dynamically create text-box with different ID, and then want to read the values on server side.如果我让你正确,你想动态创建具有不同 ID 的文本框,然后想读取服务器端的值。

Check out the following code查看以下代码

If you face any issue(s) in the code let me know.如果您在代码中遇到任何问题,请告诉我。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTextboxJavascript.aspx.cs"
  Inherits="DynamicTextboxJavascript" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

  <script type="text/javascript">
function addElement() {
    var ni = document.getElementById('myDiv');
    var numi = document.getElementById('theValue');
    var num = (document.getElementById('theValue').value -1)+2;
    numi.value = num;
    var newdiv = document.createElement('div');
    var divIdName = 'my'+num+'Div';
    newdiv.setAttribute('id',divIdName);
    newdiv.innerHTML = '<input type="text"  name="TextBox'+num+'" value="TextBox'+num+'" >';    
    ni.appendChild(newdiv);
  } 


  </script>

  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
      <div id="myDiv">
      </div>
      <input type="button" id="btnOfficial" value="Add Another TextBox" onclick="addElement();" />
      <input type="hidden" value="1" id="theValue" runat="server" />
      <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Read" />
  </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DynamicTextboxJavascript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSave_Click(object sender, EventArgs e)
{

           ArrayList alForm = new ArrayList();
        //Because my textbox id is started
       // with 2 like(TextBox2,TextBox3.....
        for (int i = 2; i< Request.Form.Count - 2;i++)
        {
            string strId = "TextBox" + i.ToString();
            string strValue = Request.Form[strId].ToString();
            alForm.Add(strValue);
            strValue = "";


     }

  GridView1.DataSource = alForm;
  GridView1.DataBind();


}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM